设置配置请求信息
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();
}
}
}
}