HttpClient实现调用外部项目接口工具类的示例
实例如下:
importjava.io.IOException;
importjava.net.URL;
importjava.util.ArrayList;
importjava.util.List;
importjava.util.Map;
importorg.apache.http.NameValuePair;
importorg.apache.http.HttpEntity;
importorg.apache.http.client.config.RequestConfig;
importorg.apache.http.client.entity.UrlEncodedFormEntity;
importorg.apache.http.client.methods.CloseableHttpResponse;
importorg.apache.http.client.methods.HttpGet;
importorg.apache.http.client.methods.HttpPost;
importorg.apache.http.conn.ssl.DefaultHostnameVerifier;
importorg.apache.http.conn.util.PublicSuffixMatcher;
importorg.apache.http.conn.util.PublicSuffixMatcherLoader;
importorg.apache.http.impl.client.CloseableHttpClient;
importorg.apache.http.impl.client.HttpClients;
importorg.apache.http.message.BasicNameValuePair;
importorg.apache.http.util.EntityUtils;
publicclassHttpUtils{
privatestaticRequestConfigrequestConfig=RequestConfig.custom().setSocketTimeout(15000).setConnectTimeout(15000)
.setConnectionRequestTimeout(15000).build();
publicstaticStringsendHttpGet(HttpGethttpGet){
CloseableHttpClienthttpClient=null;
CloseableHttpResponseresponse=null;
HttpEntityentity=null;
StringresponseContent=null;
try{
//创建默认的httpClient实例.
httpClient=HttpClients.createDefault();
httpGet.setConfig(requestConfig);
//执行请求
response=httpClient.execute(httpGet);
entity=response.getEntity();
responseContent=EntityUtils.toString(entity,"UTF-8");
}catch(Exceptione){
e.printStackTrace();
}finally{
try{
//关闭连接,释放资源
if(response!=null){
response.close();
}
if(httpClient!=null){
httpClient.close();
}
}catch(IOExceptione){
e.printStackTrace();
}
}
returnresponseContent;
}
/**
*发送post请求
*@paramhttpUrl地址
*@parammaps参数
*/
publicstaticStringsendHttpPost(StringhttpUrl,Mapmaps){
HttpPosthttpPost=newHttpPost(httpUrl);//创建httpPost
//创建参数队列
ListnameValuePairs=newArrayList();
for(Stringkey:maps.keySet()){
nameValuePairs.add(newBasicNameValuePair(key,maps.get(key)));
}
try{
httpPost.setEntity(newUrlEncodedFormEntity(nameValuePairs,"UTF-8"));
}catch(Exceptione){
e.printStackTrace();
}
returnsendHttpPost(httpPost);
}
publicstaticStringsendHttpPost(HttpPosthttpPost){
CloseableHttpClienthttpClient=null;
CloseableHttpResponseresponse=null;
HttpEntityentity=null;
StringresponseContent=null;
try{
//创建默认的httpClient实例.
httpClient=HttpClients.createDefault();
httpPost.setConfig(requestConfig);
//执行请求
response=httpClient.execute(httpPost);
entity=response.getEntity();
responseContent=EntityUtils.toString(entity,"UTF-8");
}catch(Exceptione){
e.printStackTrace();
}finally{
try{
//关闭连接,释放资源
if(response!=null){
response.close();
}
if(httpClient!=null){
httpClient.close();
}
}catch(IOExceptione){
e.printStackTrace();
}
}
returnresponseContent;
}
/**
*发送Get请求Https
*@paramhttpPost
*@return
*/
publicstaticStringsendHttpsGet(HttpGethttpGet){
CloseableHttpClienthttpClient=null;
CloseableHttpResponseresponse=null;
HttpEntityentity=null;
StringresponseContent=null;
try{
//创建默认的httpClient实例.
PublicSuffixMatcherpublicSuffixMatcher=PublicSuffixMatcherLoader.load(newURL(httpGet.getURI().toString()));
DefaultHostnameVerifierhostnameVerifier=newDefaultHostnameVerifier(publicSuffixMatcher);
httpClient=HttpClients.custom().setSSLHostnameVerifier(hostnameVerifier).build();
httpGet.setConfig(requestConfig);
//执行请求
response=httpClient.execute(httpGet);
entity=response.getEntity();
responseContent=EntityUtils.toString(entity,"UTF-8");
}catch(Exceptione){
e.printStackTrace();
}finally{
try{
//关闭连接,释放资源
if(response!=null){
response.close();
}
if(httpClient!=null){
httpClient.close();
}
}catch(IOExceptione){
e.printStackTrace();
}
}
returnresponseContent;
}
}
以上这篇HttpClient实现调用外部项目接口工具类的示例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。