Java 使用HttpClient进行模拟请求
一、 环境
jdk版本: jdk1.8+
HttpClient版本: httpClient4.5+
推荐一个Java学习的网站 : http://how2j.cn?p=17361
二、 下载jar包
1、 下载HttpClient的jar包,在apache官网
**下载地址**: [http://hc.apache.org/downloads.cgi][http_hc.apache.org_downloads.cgi]
三、 使用步骤(转)
- 创建HttpClient对象,HttpClients.createDefault()。
- 创建请求方法的实例,并指定请求URL。如果需要发送GET请求,创建HttpGet对象;如果需要发送POST请求,创建HttpPost对象,HttpPost httpPost = new HttpPost(url)。
- 如果需要发送请求参数,可调用HttpGet、HttpPost共同的setParams(HetpParams params)方法来添加请求参数;对于HttpPost对象而言,也可调用setEntity(HttpEntity entity)方法来设置请求参数。List
valuePairs = new LinkedList ();valuePairs.add(new BasicNameValuePair(entry.getKey(),entry.getValue()));httpPost.setEntity(formEntity)。 - 调用HttpClient对象的execute(HttpUriRequest request)发送请求,该方法返回一个HttpResponse。
- 调用HttpResponse的getAllHeaders()、getHeaders(String name)等方法可获取服务器的响应头;调用HttpResponse的getEntity()方法可获取HttpEntity对象,该对象包装了服务器的响应内容。程序可通过该对象获取服务器的响应内容。
释放连接。无论执行方法是否成功,都必须释放连接
HttpClient在4.3版本以后声明HttpClient的方法和以前略有区别,不再是直接声明new DefaultHttpClient() .
四、 代码示例
package util;
import org.apache.http.*;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.CookieStore;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.CookieSpecs;
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.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.NoopHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.ssl.TrustStrategy;
import org.apache.http.util.EntityUtils;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.io.*;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
/**
* 模拟请求工具类
*/
public class HttpUtil {
/**
* http get请求
*
* @param url
* @return
*/
public static String httpGet(String url) throws IOException, ClassNotFoundException, NoSuchAlgorithmException, KeyStoreException, KeyManagementException {
return httpGet(url, null, false);
}
/**、
* https get 请求
* @param url
* @return
* @throws ClassNotFoundException
* @throws KeyManagementException
* @throws NoSuchAlgorithmException
* @throws KeyStoreException
* @throws IOException
*/
public static String httpsGet(String url) throws ClassNotFoundException, KeyManagementException, NoSuchAlgorithmException, KeyStoreException, IOException {
return httpGet(url, null, true);
}
/**
* http post 请求
* @param url
* @param params
* @return
* @throws NoSuchAlgorithmException
* @throws KeyStoreException
* @throws KeyManagementException
*/
public static String httpPost(String url, HashMap<String, String> params) throws NoSuchAlgorithmException, KeyStoreException, KeyManagementException, IOException {
return httpPost(url, params, null, false);
}
/**
* https post 请求
* @param url
* @param params
* @return
* @throws NoSuchAlgorithmException
* @throws KeyStoreException
* @throws KeyManagementException
*/
public static String httpsPost(String url, HashMap<String, String> params) throws NoSuchAlgorithmException, KeyStoreException, KeyManagementException, IOException {
return httpPost(url, params, null, true);
}
/**
* @param @return 参数
* @return String 返回类型
* @throws
* @Title: httpGet
* @Description: TODO(http get请求)
*/
public static String httpGet(String url, HashMap<String, Object> maps, boolean https) throws IOException, ClassNotFoundException, KeyStoreException, NoSuchAlgorithmException, KeyManagementException {
// 创建HttpClient上下文
HttpClientContext context = HttpClientContext.create();
// 创建一个CloseableHttpClient
CloseableHttpClient httpClient = null;
if(https){
httpClient = getCloseableHttpClient();
}else{
httpClient = HttpClients.createDefault();
}
// get method
HttpGet httpGet = new HttpGet(url);
//设置header
if (maps != null) {
if (maps.containsKey(HttpHeaders.REFERER)) {
httpGet.setHeader(HttpHeaders.REFERER, (String) maps.get(HttpHeaders.REFERER));
}
if (maps.containsKey(HttpHeaders.HOST)) {
httpGet.setHeader(HttpHeaders.HOST, (String) maps.get(HttpHeaders.HOST));
}
if (maps.containsKey(HttpHeaders.USER_AGENT)) {
httpGet.setHeader(HttpHeaders.USER_AGENT, (String) maps.get(HttpHeaders.USER_AGENT));
}
if (maps.containsKey(HttpHeaders.ACCEPT)) {
httpGet.setHeader(HttpHeaders.ACCEPT, (String) maps.get(HttpHeaders.ACCEPT));
}
}
//response
CloseableHttpResponse response = null;
//get response into String
String result = "";
response = httpClient.execute(httpGet, context);
HttpEntity entity = response.getEntity();
result = EntityUtils.toString(entity, "UTF-8");
//释放连接
httpGet.releaseConnection();
httpClient.close();
return result;
}
/**
* 创建CloseableHttpClient
* @return
* @throws KeyStoreException
* @throws NoSuchAlgorithmException
* @throws KeyManagementException
*/
private static CloseableHttpClient getCloseableHttpClient() throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException {
// 全局请求设置
RequestConfig globalConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD).setConnectionRequestTimeout(10000).setConnectTimeout(10000).setSocketTimeout(10000).build();
//SSL 过滤
SSLContextBuilder builder = new SSLContextBuilder();
builder.loadTrustMaterial(null, new TrustStrategy() {
public boolean isTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
return true;
}
});
SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory(builder.build(), new String[]{"SSLv2Hello", "SSLv3", "TLSv1", "TLSv1.2"}, null, NoopHostnameVerifier.INSTANCE);
Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create().register("http", PlainConnectionSocketFactory.INSTANCE).register("https", trustAllHttpsCertificates()).build();
PoolingHttpClientConnectionManager manager = new PoolingHttpClientConnectionManager(registry);
manager.setMaxTotal(200);
// http 请求默认设置
HttpClientBuilder custom = HttpClients.custom();
custom.setDefaultRequestConfig(globalConfig);
custom.setSSLSocketFactory(sslConnectionSocketFactory);
custom.setConnectionManager(manager);
custom.setConnectionManagerShared(true);
return custom.build();
}
/**
* SSL https 构建
* @return
*/
private static SSLConnectionSocketFactory trustAllHttpsCertificates() {
SSLConnectionSocketFactory socketFactory = null;
TrustManager[] trustAllCerts = new TrustManager[1];
TrustManager tm = new miTM();
trustAllCerts[0] = tm;
SSLContext sc = null;
try {
sc = SSLContext.getInstance("TLS");//sc = SSLContext.getInstance("TLS")
sc.init(null, trustAllCerts, null);
socketFactory = new SSLConnectionSocketFactory(sc, NoopHostnameVerifier.INSTANCE);
//HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (KeyManagementException e) {
e.printStackTrace();
}
return socketFactory;
}
static class miTM implements TrustManager, X509TrustManager {
public X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkServerTrusted(X509Certificate[] certs, String authType) {
//don't check
}
public void checkClientTrusted(X509Certificate[] certs, String authType) {
//don't check
}
}
/**
* @param @return 参数
* @return String 返回类型
* @throws
* @Title: httpPost
* @Description: TODO(http post请求)
*/
public static String httpPost(String url, HashMap<String, String> params, HashMap<String, Object> config, boolean https) throws NoSuchAlgorithmException, KeyStoreException, KeyManagementException, IOException {
//httpClient
//HttpClient httpClient = getCloseableHttpClient();
HttpClient httpClient = null;
if(https){
httpClient = getCloseableHttpClient();
}else{
httpClient = HttpClients.createDefault();
}
// get method
HttpPost httpPost = new HttpPost(url);
//设置header
if (config != null) {
if (config.containsKey(HttpHeaders.REFERER)) {
httpPost.setHeader(HttpHeaders.REFERER, (String) config.get(HttpHeaders.REFERER));
}
if (config.containsKey(HttpHeaders.HOST)) {
httpPost.setHeader(HttpHeaders.HOST, (String) config.get(HttpHeaders.HOST));
}
if (config.containsKey(HttpHeaders.USER_AGENT)) {
httpPost.setHeader(HttpHeaders.USER_AGENT, (String) config.get(HttpHeaders.USER_AGENT));
}
if (config.containsKey(HttpHeaders.ACCEPT)) {
httpPost.setHeader(HttpHeaders.ACCEPT, (String) config.get(HttpHeaders.ACCEPT));
}
if(config.containsKey(HttpHeaders.CONTENT_TYPE)){
httpPost.setHeader(HttpHeaders.CONTENT_TYPE, (String) config.get(HttpHeaders.CONTENT_TYPE));
}
if(config.containsKey("Origin")){
httpPost.setHeader("Origin", (String) config.get("Origin"));
}
}
//set params post参数
List<NameValuePair> listParams = new ArrayList<NameValuePair>();
//添加post参数
for (Map.Entry<String, String> entry : params.entrySet()) {
listParams.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
String result = "";
try {
//response
httpPost.setEntity(new UrlEncodedFormEntity(listParams, "UTF-8"));
HttpResponse response = null;
response = httpClient.execute(httpPost);
//get response into String
HttpEntity entity = response.getEntity();
result = EntityUtils.toString(entity, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
httpPost.releaseConnection();
((CloseableHttpClient) httpClient).close();
}
return result;
}
}
还没有评论,来说两句吧...