基于HttpClient4.5
package com.example.demo;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpRequestRetryHandler;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* @Author: liyu.guan
* @Date: 2019/1/22 下午2:21
*/
public class HttpClientTest {
private static PoolingHttpClientConnectionManager poolConnManager = null;
private static CloseableHttpClient httpClient ;
private static RequestConfig requestConfig ;
static {
System.out.println("初始化HttpClientTest开始");
SSLContextBuilder builder = new SSLContextBuilder();
try {
builder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory(builder.build());
// 配置同时支持 HTTP 和 HTPPS
Registry socketFactoryRegistry = RegistryBuilder. create().register(
"http", PlainConnectionSocketFactory.getSocketFactory()).register(
"https", sslConnectionSocketFactory).build();
// 初始化连接管理器
poolConnManager = new PoolingHttpClientConnectionManager(
socketFactoryRegistry);
// 将最大连接数增加到200,实际项目最好从配置文件中读取这个值
poolConnManager.setMaxTotal(200);
// 设置最大路由
poolConnManager.setDefaultMaxPerRoute(2);
// 根据默认超时限制初始化requestConfig
int socketTimeout = 1000;
int connectTimeout = 1000;
int connectionRequestTimeout = 1000;
requestConfig = RequestConfig.custom().setConnectionRequestTimeout(
connectionRequestTimeout).setSocketTimeout(socketTimeout).setConnectTimeout(
connectTimeout)
.build();
// 初始化httpClient
httpClient = getConnection();
System.out.println("初始化HttpClientTest~~~结束");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (KeyStoreException e) {
e.printStackTrace();
} catch (KeyManagementException e) {
e.printStackTrace();
}
}
public static CloseableHttpClient getConnection() {
CloseableHttpClient httpClient = HttpClients.custom()
// 设置连接池管理
.setConnectionManager(poolConnManager)
// 设置请求配置
.setDefaultRequestConfig(requestConfig)
// 设置重试次数
.setRetryHandler(new DefaultHttpRequestRetryHandler(0, false))
.build();
if (poolConnManager != null && poolConnManager.getTotalStats() != null)
{
System.out.println("now client pool "
+ poolConnManager.getTotalStats().toString());
}
return httpClient;
}
private static void config(HttpRequestBase httpRequestBase) {
// 设置Header等
httpRequestBase.setHeader("User-Agent", "Mozilla/5.0");
// httpRequestBase
// .setHeader("Accept",
// "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
// httpRequestBase.setHeader("Accept-Language",
// "zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3");// "en-US,en;q=0.5");
// httpRequestBase.setHeader("Accept-Charset",
// "ISO-8859-1,utf-8,gbk,gb2312;q=0.7,*;q=0.7");
}
/**
* get 返回内容
* @param url
* @return
*/
public static String get(String url) {
HttpGet httpget = new HttpGet(url);
// 设置请求头,可以根据不同的请求设置不同的请求头 具体根据需求
config(httpget);
CloseableHttpResponse response = null;
try {
response = httpClient.execute(httpget,
HttpClientContext.create());
HttpEntity entity = response.getEntity();
String result = EntityUtils.toString(entity, "utf-8");
EntityUtils.consume(entity);
return result;
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (response != null) {
response.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
/**
* 设置post参数
* @param post
* @param paramas
*/
private static void setPostParams(HttpPost post ,Map paramas) {
List nameValuePairList = new ArrayList<>();
Set keySets = paramas.keySet();
for (String key : keySets) {
nameValuePairList.add(new BasicNameValuePair(key,paramas.get(key).toString()));
}
try {
post.setEntity(new UrlEncodedFormEntity(nameValuePairList,"utf-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
/**
* post请求获取返回内容
* @param url
* @param params
* @return
*/
public static String post(String url,Map params) throws IOException{
HttpPost httpPost = new HttpPost(url);
config(httpPost);
setPostParams(httpPost,params);
CloseableHttpResponse response = null;
try {
response = httpClient.execute(httpPost, HttpClientContext.create());
HttpEntity entity = response.getEntity();
String result = EntityUtils.toString(entity,"utf-8").toString();
// 关闭流
EntityUtils.consume(entity);
return result;
} catch (IOException e) {
throw e;
}
finally {
try {
if (response != null){
response.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
Map map = new HashMap<>();
map.put("id",11);
try {
System.out.println(post("http://localhost:8000/dologin",map));
} catch (IOException e) {
e.printStackTrace();
}
}
}