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;

}

相关推荐