HttpClient是Apache Jakarta Common下的子項(xiàng)目,用來提供高效的、最新的流酬、功能豐富的支持HTTP協(xié)議的客戶端編程工具包,并且它支持HTTP協(xié)議最新的版本和建議列另。HttpClient已經(jīng)應(yīng)用在很多的項(xiàng)目中芽腾,比如Apache Jakarta上很著名的另外兩個開源項(xiàng)目Cactus和HTMLUnit都使用了HttpClient。
下載地址: http://hc.apache.org/downloads.cgi
**發(fā)送 http 請求 **
package com.zealotpz.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.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
- Created by zealotpz on 2017/2/9.
*/
public class HttpClientTest {
//設(shè)置鏈接超時和請求超時等參數(shù)页衙,否則會長期停止或者崩潰
private static RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(60000).setConnectTimeout(60000).setConnectionRequestTimeout(60000).build();
public static String sendPost(String url, Map<String, String> params) {
String responseContent = null;
try {
//建立HttpPost對象
HttpPost httpPost = new HttpPost(url);
//建立一個NameValuePair數(shù)組摊滔,用于存儲欲傳送的參數(shù)
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
//遍歷參數(shù) map,添加參數(shù)
for (String key : params.keySet()) {
nvps.add(new BasicNameValuePair(key, params.get(key)));
}
//參數(shù)集合傳入到一個UrlEncodedFormEntity中并設(shè)置編碼
httpPost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
CloseableHttpClient httpClient = HttpClients.custom().setDefaultRequestConfig(requestConfig).build();
//發(fā)送Post,并返回一個HttpResponse對象
CloseableHttpResponse httpResponse = httpClient.execute(httpPost);
//使用響應(yīng)對象獲取響應(yīng)實(shí)
HttpEntity httpEntity = httpResponse.getEntity();
if (httpEntity != null) {
//將響應(yīng)實(shí)體轉(zhuǎn)為字符串
responseContent = EntityUtils.toString(httpEntity, "UTF-8");
}
} catch (Exception e) {
e.printStackTrace();
}
//返回應(yīng)答字符串
return responseContent;
}
**發(fā)送 https 請求 **
package com.zealotpz.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.HttpPost;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLContextBuilder;
import org.apache.http.conn.ssl.TrustStrategy;
import org.apache.http.conn.ssl.X509HostnameVerifier;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLException;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocket;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
- Created by zealotpz on 2017/2/9.
*/
public class HttpClientTest {
//設(shè)置鏈接超時和請求超時等參數(shù),否則會長期停止或者崩潰
private static RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(60000).setConnectTimeout(60000).setConnectionRequestTimeout(60000).build();
public static String sendHttpsPost(String url, Map<String, String> params) {
String responseContent = null;
try {
HttpPost httpPost = new HttpPost(url);
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
for (String key : params.keySet()) {
nvps.add(new BasicNameValuePair(key, params.get(key)));
}
httpPost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(createSSLConnSocketFactory()).setDefaultRequestConfig(requestConfig).build();
CloseableHttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
if (httpEntity != null) {
responseContent = EntityUtils.toString(httpEntity, HTTP.UTF_8);
}
} catch (Exception e) {
e.printStackTrace();
}
return responseContent;
}
//創(chuàng)建SSL安全連接
private static SSLConnectionSocketFactory createSSLConnSocketFactory() {
SSLConnectionSocketFactory sslsf = null;
try {
SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
return true;
}
}).build();
sslsf = new SSLConnectionSocketFactory(sslContext, new X509HostnameVerifier() {
public boolean verify(String arg0, SSLSession arg1) {
return true;
}
public void verify(String host, SSLSocket ssl) throws IOException {
}
public void verify(String host, X509Certificate cert) throws SSLException {
}
public void verify(String host, String[] cns, String[] subjectAlts) throws SSLException {
}
});
} catch (GeneralSecurityException e) {
e.printStackTrace();
}
return sslsf;
}
}