Httpclient 设置域名指向ip的两种方式
httpRequest是由params和url构成
privatestaticStringsend2(HttpUriRequesthttpRequest,CookieStorecookieStore)throwsIOException{
HttpClientBuilderhttpClientBuilder=HttpClients.custom();
//if(cookieStore!=null){
byteip[]=newbyte[]{(byte)123,(byte)13,12,31};
InetAddressinetAddress=InetAddress.getByAddress("mppc.siemin.com",ip);
HttpHostproxy=newHttpHost(inetAddress,9083,"http");
RequestConfigglobalConfig=RequestConfig.custom()
.setCookieSpec(CookieSpecs.DEFAULT)
.setProxy(proxy)
.build();
httpClientBuilder.setDefaultRequestConfig(globalConfig);
httpClientBuilder.setDefaultCookieStore(cookieStore);
//}
CloseableHttpClientclient=httpClientBuilder.build();
CloseableHttpResponseresponse=client.execute(httpRequest);
try{
HttpEntityentity=response.getEntity();
if(entity!=null){
returnEntityUtils.toString(entity,"UTF-8");
}
}finally{
response.close();
}
returnnull;
}
privatestaticStringsend(HttpUriRequesthttpRequest,CookieStorecookieStore)throwsIOException{
HttpClientBuilderhttpClientBuilder=HttpClients.custom();
HttpHostproxy=newHttpHost("123.21.213.213",9083,"http");
RequestConfigglobalConfig=RequestConfig.custom()
.setCookieSpec(CookieSpecs.DEFAULT)
.setProxy(proxy)
.build();
httpClientBuilder.setDefaultRequestConfig(globalConfig);
httpClientBuilder.setDefaultCookieStore(cookieStore);
CloseableHttpClientclient=httpClientBuilder.build();
CloseableHttpResponseresponse=client.execute(httpRequest);
try{
HttpEntityentity=response.getEntity();
if(entity!=null){
returnEntityUtils.toString(entity,"UTF-8");
}
}finally{
response.close();
}
returnnull;
}
相关推荐
创建一个 HttpClient 实例,这个实例需要调用 Dispose 方法释放资源,这里使用了 using 语句。接着调用 GetAsync,给它传递要调用的方法的地址,向服务器发送 Get 请求。