日期:2014-05-16 浏览次数:20991 次
最近在做相关的支付工作,接口间内部需要使用用https的方式提交数据,寻寻觅觅了很长时间通常的做法是将证书安装到本机,再使用https链接,这样极其不方便,如果我方使用的是集群的方式,即使是两个命令,那这个工作也不容忽视,毕竟现在时21世纪,能自动的就不要人工。有时人工总会出点问题!~~
废话少说了,直接上代码
?
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.security.cert.X509Certificate;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.params.ConnManagerPNames;
import org.apache.http.conn.params.ConnPerRouteBean;
import org.apache.http.conn.scheme.PlainSocketFactory;
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.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.SingleClientConnManager;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
public class HttpsTest {
public static void main(String[] args) throws Exception {
String url = "https://abc.com/usr/signin.do
";
// // 获得密匙库
// KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
// FileInputStream instream = new FileInputStream(new File(" D:/zzaa "));
// // 密匙库的密码
// trustStore.load(instream, " 123456 ".toCharArray());
// // 注册密匙库
X509TrustManager tm = new X509TrustManager() {
public void checkClientTrusted(X509Certificate[] xcs, String string) {}
public void checkServerTrusted(X509Certificate[] xcs, String string) {}
public X509Certificate[] getAcceptedIssuers() {return null;}
};
SSLContext sslcontext = SSLContext.getInstance("TLS");
sslcontext.init(null, new TrustManager[]{tm}, null);
SSLSocketFactory socketFactory = new SSLSocketFactory(sslcontext,SSLSocketFactory.STRICT_HOSTNAME_VERIFIER);
// 不校验域名
// socketFactory.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
Scheme sch = new Scheme("https", 443, socketFactory);
SchemeRegistry schemeRegistry = new SchemeRegistry();
schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
schemeRegistry.register(sch);
HttpParams params = new BasicHttpParams();
params.setParameter(ConnManagerPNames.MAX_TOTAL_CONNECTIONS, 30);
params.setParameter(ConnManagerPNames.MAX_CONNECTIONS_PER_ROUTE, new ConnPerRouteBean(30));
params.setParameter(HttpProtocolParams.USE_EXPECT_CONTINUE, false);
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
ClientConnectionManager cm = new SingleClientConnManager(params, schemeRegistry);
HttpClient httpClient = new DefaultHttpClient(cm, params);
// 获得HttpGet对象
HttpPost post = new HttpPost(url);
// 发送请求
HttpResponse response = httpClient.execute(post);
// 输出返回值
InputStream is = response.getEntity().getContent();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line = "";
while ((line = br.readLine()) != null) {
System.out.println(line);
}
}
}