import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
httpclient连接池Utils
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
httpclient连接池Utils
public class HttpClientUtils {
/**httpclient连接池Utils
* produce http client
*
* @return
* @throws KeyStoreException
* @throws NoSuchAlgorithmException
* @throws KeyManagementException
*/
public static CloseableHttpClient acceptsHttpClient() throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException {
HttpClientBuilder b = HttpClientBuilder.create();
PoolingHttpClientConnectionManager connMgr = new PoolingHttpClientConnectionManager();
connMgr.setMaxTotal(200);
connMgr.setDefaultMaxPerRoute(100);
b.setConnectionManager(connMgr);
CloseableHttpClient client = b.build();
return client;
}
}
代码片段模拟Post提交方式
CloseableHttpClient httpclient = HttpClientUitls.acceptsHttpClient();
String JSONstr = null;//求情返回的JSON数据
UrlEncodedFormEntity uefEntity;
CloseableHttpResponse closeableHttpResponse=null;
String url="url路径";
List<NameValuePair> formparams=null;
try {
HttpPost httppost = new HttpPost(url);
if (formparams == null) {//提交的参数
formparams = new ArrayList<NameValuePair>();
}
//具体需求具体参数
// formparams.add(new BasicNameValuePair("pageNumber", pageNumber.toString()));
//formparams.add(new BasicNameValuePair("pageSize", pageSize.toString()));
uefEntity = new UrlEncodedFormEntity(formparams, "UTF-8");
httppost.setEntity(uefEntity);
closeableHttpResponse = httpclient.execute(httppost);
HttpEntity entity = closeableHttpResponse.getEntity();
if (entity != null) {