Apache HttpClient4 创建HTTPS 链接并自动接受证书
最近在做相关的支付工作,接口间内部需要使用用https的方式提交数据,寻寻觅觅了很长时间通常的做法是将证书安装到本机,再使用https链接,这样极其不方便,如果我方使用的是集群的方式,即使是两个命令,那这个工作也不容忽视,毕竟现在时21世纪,能自动的就不要人工。有时人工总会出点问题!~~
废话少说了,直接上代码
import java.io.BufferedReader;
importjava.io.InputStream;
importjava.io.InputStreamReader;
import java.security.cert.X509Certificate;import javax.net.ssl.SSLContext;
importjavax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;import org.apache.http.HttpResponse;
importorg.apache.http.HttpVersion;
importorg.apache.http.client.HttpClient;
importorg.apache.http.client.methods.HttpPost;
importorg.apache.http.conn.ClientConnectionManager;
importorg.apache.http.conn.params.ConnManagerPNames;
importorg.apache.http.conn.params.ConnPerRouteBean;
importorg.apache.http.conn.scheme.PlainSocketFactory;
importorg.apache.http.conn.scheme.Scheme;
importorg.apache.http.conn.scheme.SchemeRegistry;
importorg.apache.http.conn.ssl.SSLSocketFactory;
importorg.apache.http.impl.client.DefaultHttpClient;
importorg.apache.http.impl.conn.SingleClientConnManager;
importorg.apache.http.params.BasicHttpParams;
importorg.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;public class HttpsTest {
publicstaticvoidmain(String[]args)throwsException{
Stringurl="https://abc.com/usr/signin.do";
////获得密匙库
//KeyStoretrustStore=KeyStore.getInstance(KeyStore.getDefaultType());
//FileInputStreaminstream=newFileInputStream(newFile("D:/zzaa"));
////密匙库的密码
//trustStore.load(instream,"123456".toCharArray());
////注册密匙库
X509TrustManagertm=newX509TrustManager(){
publicvoidcheckClientTrusted(X509Certificate[]xcs,Stringstring){}
publicvoidcheckServerTrusted(X509Certificate[]xcs,Stringstring){}
publicX509Certificate[]getAcceptedIssuers(){returnnull;}
};
SSLContextsslcontext=SSLContext.getInstance("TLS");
sslcontext.init(null,newTrustManager[]{tm},null);
SSLSocketFactory socketFactory = new SSLSocketFactory(sslcontext,SSLSocketFactory.STRICT_HOSTNAME_VERIFIER);// 不校验域名
//socketFactory.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
Schemesch=newScheme("https",443,socketFactory);
SchemeRegistryschemeRegistry=newSchemeRegistry();
schemeRegistry.register(newScheme("http",PlainSocketFactory.getSocketFactory(),80));
schemeRegistry.register(sch);
HttpParamsparams=newBasicHttpParams();
params.setParameter(ConnManagerPNames.MAX_TOTAL_CONNECTIONS,30);
params.setParameter(ConnManagerPNames.MAX_CONNECTIONS_PER_ROUTE,newConnPerRouteBean(30));
params.setParameter(HttpProtocolParams.USE_EXPECT_CONTINUE,false);
HttpProtocolParams.setVersion(params,HttpVersion.HTTP_1_1);
ClientConnectionManagercm=newSingleClientConnManager(params,schemeRegistry);
HttpClienthttpClient=newDefaultHttpClient(cm,params);
//获得HttpGet对象
HttpPostpost=newHttpPost(url);
//发送请求
HttpResponseresponse=httpClient.execute(post);
//输出返回值
InputStreamis=response.getEntity().getContent();
BufferedReaderbr=newBufferedReader(newInputStreamReader(is));
Stringline="";
while((line=br.readLine())!=null){
System.out.println(line);
}
}
}