package qdone.common.net;
import java.nio.charset.Charset;
import java.security.KeyStore;
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.Map.Entry;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.NameValuePair;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
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.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLContexts;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
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 org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import qdone.common.lang.CharEncoding;
public class HttpUtils {
private static final Logger logger = LoggerFactory.getLogger(HttpUtils.class);
private static TrustManager truseAllManager = new X509TrustManager() {
public void checkClientTrusted(java.security.cert.X509Certificate[] arg0, String arg1)
throws CertificateException {
}
public void checkServerTrusted(java.security.cert.X509Certificate[] arg0, String arg1)
throws CertificateException {
}
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
};
public static byte[] getBytes(String url) throws Exception {
HttpGet httpGet = new HttpGet(url);
httpGet.setHeader("User-Agent","Fiddler");
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity entityResponse = httpResponse.getEntity();
if (entityResponse == null) {
return null;
}
return EntityUtils.toByteArray(entityResponse);
}
public static String getString(String url) throws Exception {
CloseableHttpClient httpClient = null;
SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
return true;
}
}).build();
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext);
try {
httpClient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
HttpGet httpGet = new HttpGet(url);
httpGet.setHeader("User-Agent","Fiddler");
CloseableHttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity entityResponse = httpResponse.getEntity();
if (entityResponse == null) {
return null;
}
return EntityUtils.toString(entityResponse);
} finally {
httpClient.close();
}
}
public static byte[] post(String url, byte[] data) throws Exception {
CloseableHttpClient httpClient = null;
SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
return true;
}
}).build();
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext);
try {
httpClient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
//
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new ByteArrayEntity(data));
CloseableHttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity entityResponse = httpResponse.getEntity();
if (entityResponse == null) {
return null;
}
return EntityUtils.toByteArray(entityResponse);
} finally {
httpClient.close();
}
}
public static byte[] post(String url, byte[] data,String proxyHost,int proxyPort) throws Exception {
CloseableHttpClient httpClient = null;
SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
return true;
}
}).build();
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext);
try {
HttpHost proxy = new HttpHost(proxyHost,proxyPort);
httpClient = HttpClients.custom().setSSLSocketFactory(sslsf)
.setProxy(proxy)
.build();
//
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new ByteArrayEntity(data));
CloseableHttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity entityResponse = httpResponse.getEntity();
if (entityResponse == null) {
return null;
}
return EntityUtils.toByteArray(entityResponse);
} finally {
httpClient.close();
}
}
public static byte[] clientPost(String url, byte[] data,String proxyHost,int proxyPort) throws Exception {
CloseableHttpClient httpClient = null;
SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
return true;
}
}).build();
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext);
try {
HttpHost proxy = new HttpHost(proxyHost,proxyPort);
httpClient = HttpClients.custom().setSSLSocketFactory(sslsf)
.setProxy(proxy)
.build();
//
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new ByteArrayEntity(data));
httpPost.setHeader("Content-Type", "text/xml;charset=UTF-8");
CloseableHttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity entityResponse = httpResponse.getEntity();
if (entityResponse == null) {
return null;
}
return EntityUtils.toByteArray(entityResponse);
} finally {
httpClient.close();
}
}
public static byte[] sslClientPost(String[] TLSv1,String mchId,KeyStore keyStore,String url, byte[] data,String proxyHost,int proxyPort) throws Exception {
CloseableHttpClient httpClient = null;
SSLContext sslcontext = SSLContexts.custom().loadKeyMaterial(keyStore,mchId.toCharArray()).build();
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
sslcontext, new String[] { "TLSv1" }, null,SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
try {
HttpHost proxy = new HttpHost(proxyHost,proxyPort);
httpClient = HttpClients.custom().setSSLSocketFactory(sslsf)
.setProxy(proxy)
.build();
//
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new ByteArrayEntity(data));
httpPost.setHeader("Content-Type", "text/xml;charset=UTF-8");
CloseableHttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity entityResponse = httpResponse.getEntity();
if (entityResponse == null) {
return null;
}
return EntityUtils.toByteArray(entityResponse);
} finally {
httpClient.close();
}
}
public static byte[] sslClientPost(String[] TLSv1,String mchId,KeyStore keyStore,String url, byte[] data,String proxyHost,int proxyPort,String username,String pass) throws Exception {
CloseableHttpClient httpClient = null;
SSLContext sslcontext = SSLContexts.custom().loadKeyMaterial(keyStore,mchId.toCharArray()).build();
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
sslcontext, new String[] { "TLSv1" }, null,SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
try {
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(new AuthScope(proxyHost,proxyPort), // 可以访问的范围
new UsernamePasswordCredentials(username,pass));// 用户名和密码
HttpHost proxy = new HttpHost(proxyHost,proxyPort);
httpClient = HttpClients.custom().setSSLSocketFactory(sslsf)
.setProxy(proxy)
.setDefaultCredentialsProvider(credsProvider)
.build();
//
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new ByteArrayEntity(data));
httpPost.setHeader("Content-Type", "text/xml;charset=UTF-8");
CloseableHttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity entityResponse = httpResponse.getEntity();
if (entityResponse == null) {
return null;
}
return EntityUtils.toByteArray(entityResponse);
} finally {
httpClient.close();
}
}
public static byte[] sslClientPost(String[] TLSv1,String mchId,KeyStore keyStore,String url, byte[] data) throws Exception {
CloseableHttpClient httpClient = null;
SSLContext sslcontext = SSLContexts.custom().loadKeyMaterial(keyStore,mchId.toCharArray()).build();
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
sslcontext, new String[] { "TLSv1" }, null,SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
try {
httpClient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new ByteArrayEntity(data));
httpPost.setHeader("Content-Type", "text/xml;charset=UTF-8");
CloseableHttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity entityResponse = httpResponse.getEntity();
if (entityResponse == null) {
return null;
}
return EntityUtils.toByteArray(entityResponse);
} finally {
httpClient.close();
}
}
public static byte[] clientPost(String url, byte[] data,String proxyHost,int proxyPort,String username,String pass) throws Exception {
CloseableHttpClient httpClient = null;
SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
return true;
}
}).build();
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext);
try {
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(new AuthScope(proxyHost,proxyPort), // 可以访问的范围
new UsernamePasswordCredentials(username,pass));// 用户名和密码
HttpHost proxy = new HttpHost(proxyHost,proxyPort);
httpClient = HttpClients.custom().setSSLSocketFactory(sslsf)
.setProxy(proxy)
.setDefaultCredentialsProvider(credsProvider)
.build();
//
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new ByteArrayEntity(data));
httpPost.setHeader("Content-Type", "text/xml;charset=UTF-8");
CloseableHttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity entityResponse = httpResponse.getEntity();
if (entityResponse == null) {
return null;
}
return EntityUtils.toByteArray(entityResponse);
} finally {
httpClient.close();
}
}
public static byte[] clientPost(String url, byte[] data) throws Exception {
CloseableHttpClient httpClient = null;
SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
return true;
}
}).build();
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext);
try {
httpClient = HttpClients.custom().setSSLSocketFactory(sslsf)
.build();
//
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new ByteArrayEntity(data));
httpPost.setHeader("Content-Type", "text/xml;charset=UTF-8");
CloseableHttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity entityResponse = httpResponse.getEntity();
if (entityResponse == null) {
return null;
}
return EntityUtils.toByteArray(entityResponse);
} finally {
httpClient.close();
}
}
public static byte[] post(String url, byte[] data,String proxyHost,int proxyPort,String username,String pass) throws Exception {
CloseableHttpClient httpClient = null;
SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
return true;
}
}).build();
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext);
try {
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(new AuthScope(proxyHost,proxyPort), // 可以访问的范围
new UsernamePasswordCredentials(username,pass));// 用户名和密码
HttpHost proxy = new HttpHost(proxyHost,proxyPort);
httpClient = HttpClients.custom().setSSLSocketFactory(sslsf)
.setProxy(proxy)
.setDefaultCredentialsProvider(credsProvider)
.build();
//
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new ByteArrayEntity(data));
CloseableHttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity entityResponse = httpResponse.getEntity();
if (entityResponse == null) {
return null;
}
return EntityUtils.toByteArray(entityResponse);
} finally {
httpClient.close();
}
}
public static String post(String url, String data) throws Exception {
byte[] responseBytes = post(url, data.getBytes(CharEncoding.UTF8));
return new String(responseBytes, CharEncoding.UTF8);
}
public static String post(String url, String data,Charset encoding) throws Exception {
byte[] responseBytes = post(url, data.getBytes(encoding));
return new String(responseBytes, encoding);
}
public static String post(String url, String data,String encoding) throws Exception {
byte[] responseBytes = post(url, data.getBytes(encoding));
return new String(responseBytes, encoding);
}
/**
*
* @param url
* @param data
* @param proxyHost
* @param proxyPort
* @param username
* @param pass
* @return
* @throws Exception
*/
public static String post(String url, String data,String proxyHost,int proxyPort,String username,String pass) throws Exception {
byte[] responseBytes = post(url, data.getBytes(CharEncoding.UTF8),proxyHost,proxyPort, username, pass);
return new String(responseBytes, CharEncoding.UTF8);
}
/**
* 使用代理发送请求
* @param url
* @param data
* @param proxyHost 主机
* @param proxyPort 端口号
* @return
* @throws Exception
*/
public static String post(String url, String data,String proxyHost,int proxyPort) throws Exception {
byte[] responseBytes = post(url, data.getBytes(CharEncoding.UTF8),proxyHost,proxyPort);
return new String(responseBytes, CharEncoding.UTF8);
}
public static byte[] clientPost(String url, String data,String proxyHost,int proxyPort) throws Exception {
byte[] responseBytes = clientPost(url, data.getBytes(CharEncoding.UTF8),proxyHost,proxyPort);
return responseBytes;
}
public static byte[] clientPost(String url, String data,String proxyHost,int proxyPort,String username,String pass) throws Exception {
byte[] responseBytes = clientPost(url, data.getBytes(CharEncoding.UTF8),proxyHost,proxyPort, username, pass);
return responseBytes;
}
public static byte[] clientPost(String url, String data) throws Exception {
byte[] responseBytes = clientPost(url, data.getBytes(CharEncoding.UTF8));
return responseBytes;
}
public static byte[] sslClientPost(String[] TLSv1,String mchId,KeyStore keyStore,String url, String data,String proxyHost,int proxyPort) throws Exception {
byte[] responseBytes = sslClientPost(TLSv1,mchId,keyStore,url, data.getBytes(CharEncoding.UTF8),proxyHost,proxyPort);
return responseBytes;
}
public static byte[] sslClientPost(String[] TLSv1,String mchId,KeyStore keyStore,String url, String data,String proxyHost,int proxyPort,String username,String pass) throws Exception {
byte[] responseBytes = sslClientPost(TLSv1,mchId,keyStore,url, data.getBytes(CharEncoding.UTF8),proxyHost,proxyPort,username,pass);
return responseBytes;
}
public static byte[] sslClientPost(String[] TLSv1,String mchId,KeyStore keyStore,String url, String data) throws Exception {
byte[] responseBytes = sslClientPost(TLSv1,mchId,keyStore,url, data.getBytes(CharEncoding.UTF8));
return responseBytes;
}
public static byte[] postFormMap(String url, Map<String, String> params,String encoding) throws Exception {
CloseableHttpClient httpClient = null;
SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
return true;
}
}).build();
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext);
try {
httpClient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
//
HttpPost httpPost = new HttpPost(url);
List<NameValuePair> formparams = new ArrayList<NameValuePair>();
for (Entry<String, String> entry : params.entrySet()) {
formparams.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
httpPost.setEntity(new UrlEncodedFormEntity(formparams, encoding));
httpPost.setHeader("Content-type", "application/x-www-form-urlencoded");
CloseableHttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity entityResponse = httpResponse.getEntity();
if (entityResponse == null) {
return null;
}
return EntityUtils.toByteArray(entityResponse);
} finally {
httpClient.close();
}
}
public static byte[] postFormMap(String url, Map<String, String> params) throws Exception {
return postFormMap(url,params,CharEncoding.UTF8);
}
public static String postFrontServer(String url, String data) throws Exception {
Map<String, String> params = new HashMap<String, String>();
params.put("data", data);
byte[] respBytes = HttpUtils.postFormMap(url, params);
if (respBytes == null)
return null;
String respString = new String(respBytes, CharEncoding.UTF8);
return respString;
}
/**
* 使用代理发送请求
* @param url
* @param data
* @param proxyHost 主机
* @param proxyPort 端口号
* @return
* @throws Exception
*/
public static String post(String url, String data,Charset encoding,String proxyHost,int proxyPort) throws Exception {
byte[] responseBytes = post(url, data.getBytes(encoding),proxyHost,proxyPort);
return new String(responseBytes,encoding);
}
/**
*
* @param url
* @param data
* @param proxyHost
* @param proxyPort
* @param username
* @param pass
* @return
* @throws Exception
*/
public static String post(String url, String data,Charset encoding,String proxyHost,int proxyPort,String username,String pass) throws Exception {
byte[] responseBytes = post(url, data.getBytes(encoding),proxyHost,proxyPort, username, pass);
return new String(responseBytes,encoding);
}
/**
* postFrontServer2
* @param url
* @param data
* @param proxyHost
* @param proxyPort
* @return
* @throws Exception
*/
public static String postFrontServer(String url, String data,String proxyHost,int proxyPort) throws Exception {
Map<String, String> params = new HashMap<String, String>();
params.put("data", data);
byte[] respBytes = HttpUtils.postFormMapPos(url, params,proxyHost,proxyPort);
if (respBytes == null)
return null;
String respString = new String(respBytes, CharEncoding.UTF8);
return respString;
}
public static byte[] postFormMapPos(String url, Map<String, String> params,String proxyHost,int proxyPort) throws Exception {
return postFormMapPos(url,params,CharEncoding.UTF8,proxyHost,proxyPort);
}
public static byte[] postFormMapPos(String url, Map<String, String> params,String encoding,String proxyHost,int proxyPort) throws Exception {
CloseableHttpClient httpClient = null;
SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
return true;
}
}).build();
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext);
try {
HttpHost proxy = new HttpHost(proxyHost,proxyPort);
httpClient = HttpClients.custom().setSSLSocketFactory(sslsf).setProxy(proxy).build();
//
HttpPost httpPost = new HttpPost(url);
List<NameValuePair> formparams = new ArrayList<NameValuePair>();
for (Entry<String, String> entry : params.entrySet()) {
formparams.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
httpPost.setEntity(new UrlEncodedFormEntity(formparams, encoding));
httpPost.setHeader("Content-type", "application/x-www-form-urlencoded");
CloseableHttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity entityResponse = httpResponse.getEntity();
if (entityResponse == null) {
return null;
}
return EntityUtils.toByteArray(entityResponse);
} finally {
httpClient.close();
}
}
/**
* postFrontServer3
* @param url
* @param data
* @param proxyHost
* @param proxyPort
* @return
* @throws Exception
*/
public static String postFrontServer(String url, String data,String proxyHost,int proxyPort,String username,String pass) throws Exception {
Map<String, String> params = new HashMap<String, String>();
params.put("data", data);
byte[] respBytes = HttpUtils.postFormMapHttp(url, params,proxyHost,proxyPort,username,pass);
if (respBytes == null)
return null;
String respString = new String(respBytes, CharEncoding.UTF8);
return respString;
}
public static byte[] postFormMapHttp(String url, Map<String, String> params,String proxyHost,int proxyPort,String username,String pass) throws Exception {
return postFormMapHttp(url,params,CharEncoding.UTF8,proxyHost,proxyPort,username,pass);
}
public static byte[] postFormMapHttp(String url, Map<String, String> params,String encoding,String proxyHost,int proxyPort,String username,String pass) throws Exception {
CloseableHttpClient httpClient = null;
SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
return true;
}
}).build();
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext);
try {
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(new AuthScope(proxyHost,proxyPort), // 可以访问的范围
new UsernamePasswordCredentials(username,pass));// 用户名和密码
HttpHost proxy = new HttpHost(proxyHost,proxyPort);
httpClient = HttpClients.custom().setSSLSocketFactory(sslsf).setProxy(proxy).setDefaultCredentialsProvider(credsProvider).build();
HttpPost httpPost = new HttpPost(url);
List<NameValuePair> formparams = new ArrayList<NameValuePair>();
for (Entry<String, String> entry : params.entrySet()) {
formparams.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
httpPost.setEntity(new UrlEncodedFormEntity(formparams, encoding));
httpPost.setHeader("Content-type", "application/x-www-form-urlencoded");
CloseableHttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity entityResponse = httpResponse.getEntity();
if (entityResponse == null) {
return null;
}
return EntityUtils.toByteArray(entityResponse);
} finally {
httpClient.close();
}
}
public static byte[] postFormMap(String url, Map<String, String> params,String encoding,String proxyHost,int proxyPort,String username,String pass) throws Exception {
CloseableHttpClient httpClient = null;
SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
return true;
}
}).build();
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext);
try {
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(new AuthScope(proxyHost,proxyPort), // 可以访问的范围
new UsernamePasswordCredentials(username,pass));// 用户名和密码
HttpHost proxy = new HttpHost(proxyHost,proxyPort);
httpClient = HttpClients.custom().setSSLSocketFactory(sslsf).setProxy(proxy).setDefaultCredentialsProvider(credsProvider).build();
//
HttpPost httpPost = new HttpPost(url);
List<NameValuePair> formparams = new ArrayList<NameValuePair>();
for (Entry<String, String> entry : params.entrySet()) {
formparams.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
httpPost.setEntity(new UrlEncodedFormEntity(formparams, encoding));
httpPost.setHeader("Content-type", "application/x-www-form-urlencoded");
CloseableHttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity entityResponse = httpResponse.getEntity();
if (entityResponse == null) {
return null;
}
return EntityUtils.toByteArray(entityResponse);
} finally {
httpClient.close();
}
}
public static byte[] postFormMap(String url, Map<String, String> params,String encoding,String proxyHost,int proxyPort) throws Exception {
CloseableHttpClient httpClient = null;
SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
return true;
}
}).build();
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext);
try {
HttpHost proxy = new HttpHost(proxyHost,proxyPort);
httpClient = HttpClients.custom().setSSLSocketFactory(sslsf).setProxy(proxy).build();
//
HttpPost httpPost = new HttpPost(url);
List<NameValuePair> formparams = new ArrayList<NameValuePair>();
for (Entry<String, String> entry : params.entrySet()) {
formparams.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
httpPost.setEntity(new UrlEncodedFormEntity(formparams, encoding));
httpPost.setHeader("Content-type", "application/x-www-form-urlencoded");
CloseableHttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity entityResponse = httpResponse.getEntity();
if (entityResponse == null) {
return null;
}
return EntityUtils.toByteArray(entityResponse);
} finally {
httpClient.close();
}
}
public static String getString(String url,String proxyHost,int proxyPort) throws Exception {
CloseableHttpClient httpClient = null;
SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
return true;
}
}).build();
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext);
try {
HttpHost proxy = new HttpHost(proxyHost,proxyPort);
httpClient = HttpClients.custom().setSSLSocketFactory(sslsf).setProxy(proxy).build();
HttpGet httpGet = new HttpGet(url);
httpGet.setHeader("User-Agent","Fiddler");
CloseableHttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity entityResponse = httpResponse.getEntity();
if (entityResponse == null) {
return null;
}
return EntityUtils.toString(entityResponse);
} finally {
httpClient.close();
}
}
public static String getString(String url,String proxyHost,int proxyPort,String username,String pass) throws Exception {
CloseableHttpClient httpClient = null;
SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
return true;
}
}).build();
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext);
try {
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(new AuthScope(proxyHost,proxyPort), // 可以访问的范围
new UsernamePasswordCredentials(username,pass));// 用户名和密码
HttpHost proxy = new HttpHost(proxyHost,proxyPort);
httpClient = HttpClients.custom().setSSLSocketFactory(sslsf).setProxy(proxy).setDefaultCredentialsProvider(credsProvider).build();
HttpGet httpGet = new HttpGet(url);
httpGet.setHeader("User-Agent","Fiddler");
CloseableHttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity entityResponse = httpResponse.getEntity();
if (entityResponse == null) {
return null;
}
return EntityUtils.toString(entityResponse);
} finally {
httpClient.close();
}
}
}