本文主要是對http和https 發送post請求所做工具類, 方法中有兩個參數:https(是否是https地址)和proxy(是否使用代理)。
http和https主要使用apache的基礎jar包,代理地址可從配置文件中獲取。好了,廢話不多說,直接上代碼:
1. https請求類
public class HttpProxyPost {
private static Logger logger = LoggerFactory.getLogger(HttpProxyPost.class);
private static PropertiesConfiguration config = PropertiesUtils.INSTANCE.getConfig();
private static CloseableHttpClient client = null;
private static Logger logger = LoggerFactory.getLogger(HttpProxyPost.class);
private static PropertiesConfiguration config = PropertiesUtils.INSTANCE.getConfig();
private static CloseableHttpClient client = null;
public static String requestWithPost(String postUrl, JSONObject reqJson, Boolean https, Boolean proxy) {
HttpResponse rsp = null;
try {
client = SSLClient.getHttpsClient(https);
} catch (Exception e) {
logger.error("獲取httpCliet失敗");
}
HttpPost post = new HttpPost(postUrl);
try {
client = SSLClient.getHttpsClient(https);
} catch (Exception e) {
logger.error("獲取httpCliet失敗");
}
HttpPost post = new HttpPost(postUrl);
if (proxy) {
String proxyIP = config.getString("proxyIP");
int proxyPort = Integer.parseInt(config.getString("proxyPort"));
logger.info("使用代理發送請求:{}:{}", proxyIP, proxyPort);
HttpHost proxyHost = new HttpHost(proxyIP, proxyPort);
RequestConfig reqConfig = RequestConfig.custom().setProxy(proxyHost).build();
post.setConfig(reqConfig);
}
StringEntity params = null;
String rspStr = "";
String proxyIP = config.getString("proxyIP");
int proxyPort = Integer.parseInt(config.getString("proxyPort"));
logger.info("使用代理發送請求:{}:{}", proxyIP, proxyPort);
HttpHost proxyHost = new HttpHost(proxyIP, proxyPort);
RequestConfig reqConfig = RequestConfig.custom().setProxy(proxyHost).build();
post.setConfig(reqConfig);
}
StringEntity params = null;
String rspStr = "";
try {
params = new StringEntity(reqJson.toString(), "utf-8");
params.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
params.setChunked(true);
post.addHeader("content-type", "application/json");
post.setEntity(params);
rsp = client.execute(post);
HttpEntity entity = rsp.getEntity();
String rspString = EntityUtils.toString(entity, "utf-8");
JSONObject rspJson = JSONObject.parseObject(rspString);
logger.info("請求返回報文:{}", rspJson);
rspStr = rspJson.toString();
} catch (Exception e) {
logger.info("外調異常::{}", e.getMessage());
} finally {
client.getConnectionManager().shutdown();
}
return rspStr;
}
}
params = new StringEntity(reqJson.toString(), "utf-8");
params.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
params.setChunked(true);
post.addHeader("content-type", "application/json");
post.setEntity(params);
rsp = client.execute(post);
HttpEntity entity = rsp.getEntity();
String rspString = EntityUtils.toString(entity, "utf-8");
JSONObject rspJson = JSONObject.parseObject(rspString);
logger.info("請求返回報文:{}", rspJson);
rspStr = rspJson.toString();
} catch (Exception e) {
logger.info("外調異常::{}", e.getMessage());
} finally {
client.getConnectionManager().shutdown();
}
return rspStr;
}
}
2. 創建https client
public class SSLClient {
private static HttpClientBuilder builder;
public static CloseableHttpClient getHttpsClient(Boolean https)
throws KeyManagementException, NoSuchAlgorithmException {
if (builder == null) {
builder = HttpClientBuilder.create();
}
if (!https) {
return builder.build();
} else {
X509TrustManager tm = new X509TrustManager() {
throws KeyManagementException, NoSuchAlgorithmException {
if (builder == null) {
builder = HttpClientBuilder.create();
}
if (!https) {
return builder.build();
} else {
X509TrustManager tm = new X509TrustManager() {
@Override
public X509Certificate[] getAcceptedIssuers() {
return null;
}
public X509Certificate[] getAcceptedIssuers() {
return null;
}
@Override
public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
// TODO Auto-generated method stub
public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
// TODO Auto-generated method stub
}
@Override
public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
// TODO Auto-generated method stub
}
};
SSLContext ctx = SSLContext.getInstance("TLS");
ctx.init(null, new TrustManager[] { tm }, null);
SSLSocketFactory ssf = new SSLSocketFactory(ctx, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
builder.setSSLSocketFactory(ssf);
}
return builder.build();
public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
// TODO Auto-generated method stub
}
};
SSLContext ctx = SSLContext.getInstance("TLS");
ctx.init(null, new TrustManager[] { tm }, null);
SSLSocketFactory ssf = new SSLSocketFactory(ctx, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
builder.setSSLSocketFactory(ssf);
}
return builder.build();
}