1. 程式人生 > >httpclient 跳過SSL證書校驗

httpclient 跳過SSL證書校驗

1、SSLClient

package com.fxiaoke.qa.util;

import java.io.IOException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import javax.net.ssl.*;

import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.conn.ssl.X509HostnameVerifier;
import org.apache.http.impl.client.DefaultHttpClient;

/**
* 用於進行Https請求的HttpClient
* @ClassName: SSLClient
* @Description: TODO
* @author Devin <xxx>
* @date 2017年2月7日 下午1:42:07
*
*/
public class SSLClient extends DefaultHttpClient
{
public SSLSocketFactory ssf;
public SSLClient() throws Exception{
super();
SSLContext ctx = SSLContext.getInstance("TLS");
X509TrustManager tm = new X509TrustManager() {

@Override
public void checkClientTrusted(X509Certificate[] chain,
String authType) throws CertificateException {
}
@Override
public void checkServerTrusted(X509Certificate[] chain,
String authType) throws CertificateException {
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return null;
}
};
X509HostnameVerifier hostnameVerifier = new X509HostnameVerifier() {
public boolean verify(String arg0, SSLSession arg1) {
return true;
}
public void verify(String arg0, SSLSocket arg1) throws IOException
{}
public void verify(String arg0, String[] arg1, String[] arg2) throws SSLException {}
public void verify(String arg0, X509Certificate arg1) throws SSLException {}
};

ctx.init(null, new TrustManager[]{tm}, null);
ssf =new SSLSocketFactory(ctx,SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
ssf.setHostnameVerifier(hostnameVerifier);
ClientConnectionManager ccm = this.getConnectionManager();
SchemeRegistry sr = ccm.getSchemeRegistry();
sr.register(new Scheme("https", 443, ssf));
}

public SSLSocketFactory getSSF()
{
return ssf;
}
}


2、httpPostRaw方法
public static ResponseVo httpPostRaw(RequestVo requestvo) {
ResponseVo responsevo = new ResponseVo();
SSLClient httpClient = null;
HttpPost httpPost = null;
JSONObject json = new JSONObject();
HttpContext localContext = new BasicHttpContext();
try {
// httpClient = HttpClients.createDefault();
httpClient = new SSLClient();
httpClient.getConnectionManager().getSchemeRegistry().register(
new Scheme("https", httpClient.getSSF(), 443));////修改部分


RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(60000).setConnectTimeout(60000).build();
httpPost = new HttpPost(requestvo.getRequestUrl());
addAllHeaders(httpPost, requestvo.getRequestHeaders());
httpPost.setConfig(requestConfig);
setAllBody(httpPost, requestvo);
long startTime = System.currentTimeMillis();
CloseableHttpResponse response = httpClient.execute(httpPost, localContext);
long endTime = System.currentTimeMillis();
ReportUtil.log("Response time is :" + (endTime - startTime) + "ms");
@SuppressWarnings("deprecation") CookieStore cookieStore = (CookieStore) localContext.getAttribute(ClientContext.COOKIE_STORE);
// responsevo.setCookies(cookieStore.getCookies());
responsevo.setCookies(httpClient.getCookieStore().getCookies());
int responseCode = response.getStatusLine().getStatusCode();
responsevo.setHttpCode(responseCode);

if (responseCode == 200 || responseCode == 201) {
HttpEntity entity = response.getEntity();
if (entity != null) {
if (requestvo.getRequestUrl().substring(0,45).contains("FHE")) {
String responseStr = EntityUtils.toString(entity,"UTF-8");
responsevo.setResponseStr(responseStr);
ReportUtil.log("Response is:" + responseStr);
} else {
json = JSON.parseObject(EntityUtils.toString(entity,"UTF-8"));
responsevo.setJson(json);
ReportUtil.log("Response is:" + json.toJSONString());
}
} else {
ReportUtil.log("entity is null");
}
} else {
//modify by zhangjuan 修改404返回不能校驗錯誤
// ReportUtil.log(EntityUtils.toString(response.getEntity()));
String responseStr = EntityUtils.toString(response.getEntity(),"UTF-8");
ReportUtil.log(responseStr);
responsevo.setResponseStr(responseStr);
ReportUtil.log("http POST request return error, error code is " + responseCode);
}
httpPost.abort();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (httpPost != null) {
httpPost.releaseConnection();
}
if (httpClient != null) {
httpClient.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
return responsevo;
}