httpclient访问https
2016-12-15 14:21
351 查看
import java.io.IOException; import java.net.InetAddress; import java.net.InetSocketAddress; import java.net.Socket; import java.net.SocketAddress; import java.net.UnknownHostException; import java.security.KeyManagementException; import java.security.NoSuchAlgorithmException; import java.security.cert.X509Certificate; import javax.net.SocketFactory; import javax.net.ssl.SSLContext; import javax.net.ssl.TrustManager; import javax.net.ssl.X509TrustManager; import org.apache.commons.httpclient.ConnectTimeoutException; import org.apache.commons.httpclient.params.HttpConnectionParams; import org.apache.commons.httpclient.protocol.ProtocolSocketFactory; public class HTTPSSecureProtocolSocketFactory implements ProtocolSocketFactory {// SecureProtocolSocketFactory private SSLContext sslcontext = null; private SSLContext createSSLContext() { SSLContext sslcontext = null; try { sslcontext = SSLContext.getInstance("SSL"); sslcontext.init(null, new TrustManager[] { new TrustAnyTrustManager() }, new java.security.SecureRandom()); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (KeyManagementException e) { e.printStackTrace(); } return sslcontext; } private SSLContext getSSLContext() { if (null == this.sslcontext) { this.sslcontext = createSSLContext(); } return this.sslcontext; } public Socket createSocket(Socket socket, String host, int port, boolean autoClose) throws IOException, UnknownHostException { return getSSLContext().getSocketFactory().createSocket(socket, host, port, autoClose); } public Socket createSocket(String host, int port) throws IOException, UnknownHostException { return getSSLContext().getSocketFactory().createSocket(host, port); } public Socket createSocket(String host, int port, InetAddress clientHost, int clientPort) throws IOException, UnknownHostException { return getSSLContext().getSocketFactory().createSocket(host, port, clientHost, clientPort); } public Socket createSocket(String host, int port, InetAddress localAddress, int localPort, HttpConnectionParams params) throws IOException, UnknownHostException, ConnectTimeoutException { if (params == null) { throw new IllegalArgumentException("Parameters may not be null"); } int timeout = params.getConnectionTimeout(); SocketFactory socketfactory = getSSLContext().getSocketFactory(); if (timeout == 0) { return socketfactory.createSocket(host, port, localAddress, localPort); } else { Socket socket = socketfactory.createSocket(); SocketAddress localaddr = new InetSocketAddress(localAddress, localPort); SocketAddress remoteaddr = new InetSocketAddress(host, port); socket.bind(localaddr); socket.connect(remoteaddr, timeout); return socket; } } private static class TrustAnyTrustManager implements X509TrustManager { public void checkClientTrusted(X509Certificate[] chain, String authType) { } public void checkServerTrusted(X509Certificate[] chain, String authType) { } public X509Certificate[] getAcceptedIssuers() { return new X509Certificate[] {}; } } }
private static String doPost(String url) throws Exception { Protocol https = new Protocol("https", new HTTPSSecureProtocolSocketFactory(), 443); Protocol.registerProtocol("https", https); PostMethod post = new PostMethod(url); HTTPCLIENT.executeMethod(post); InputStream inputStream = post.getResponseBodyAsStream(); Protocol.unregisterProtocol("https"); BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, "utf-8")); String ss = ""; String s = null; while((s = br.readLine()) != null) { ss += s; } return ss; }
相关文章推荐
- Httpclient 访问https
- HttpClient 访问 https 出现peer can't
- HttpClient访问https站点证书加载问题
- HttpClient4.x调用指定证书访问https
- CloseableHttpClient加载证书来访问https网站
- HttpClient忽略证书访问HTTPS接口
- java httpclient 无证书访问 https
- android httpClient 支持HTTPS的访问方式
- Android进阶(三)android httpClient 支持HTTPS的访问方式
- HttpClient忽略证书访问HTTPS接口
- 如何访问https的网站?-【httpclient】
- Java 程序(httpclient) 访问https保护的网站
- HttpClient 使用代理访问Https【转】
- CloseableHttpClient加载证书来访问https网站
- Use java.net.HttpURLConnection/HttpClient 访问web service ( HTTP/HTTPS)
- CloseableHttpClient加载证书来访问https网站(转载)
- android httpClient 支持HTTPS的访问方式
- HttpClient忽略证书访问HTTPS接口
- httpclient 访问https应用
- httpclient 访问https