设置配置请求信息
public class HttpConfigTest {
public static void main(String[] args) {
//创建httpclient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
//创建httpget对象, 设置url访问地址
HttpGet httpGet = new HttpGet("https://www.ziroom.com/?utm_source=pinzhuan&utm_medium=baidu&utm_term=ziru&utm_content=biaoti&utm_campaign=pinzhuan");
//配置请求信息
RequestConfig config = RequestConfig.custom().setConnectTimeout(1000) //创建连接的最长时间,单位是毫秒
.setConnectionRequestTimeout(500) //设置获取连接的最长时间
.setSocketTimeout(10*1000) //设置数据传输的最长时间
.build();
//给请求设置请求信息
httpGet.setConfig(config);
//使用httpclient发起请求
CloseableHttpResponse response = null;
try {
response = httpClient.execute(httpGet);
//解析响应
if(response.getStatusLine().getStatusCode()==200){
String content = EntityUtils.toString(response.getEntity(),"utf8");
System.out.println(content.length());
}
} catch (IOException e) {
e.printStackTrace();
}finally {
//关闭response
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

本文介绍了一个使用Java进行HTTP请求配置的具体实例,展示了如何通过设置连接超时、获取连接超时及数据传输超时来定制HTTP请求的行为。通过该示例,读者可以了解如何有效地管理和优化网络请求。
1万+

被折叠的 条评论
为什么被折叠?



