java https请求
来源:互联网 发布:套淘宝保证金如何秒回 编辑:程序博客网 时间:2024/06/06 19:14
package com.ccop.common.util;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.CertificateException;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) throws CertificateException { } public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { } public X509Certificate[] getAcceptedIssuers() { return new X509Certificate[] {}; } } }
public static String getStringByHttps(String url) { String result = ""; Protocol https = new Protocol("https", new HTTPSSecureProtocolSocketFactory(), 443); Protocol.registerProtocol("https", https); GetMethod get = new GetMethod(url); org.apache.commons.httpclient.HttpClient client = new org.apache.commons.httpclient.HttpClient(); try { client.executeMethod(get); result = get.getResponseBodyAsString(); result = new String(result.getBytes("ISO-8859-1"), "UTF-8"); Protocol.unregisterProtocol("https"); return result; } catch(Exception e) { e.printStackTrace(); } return "error"; }
阅读全文
0 0
- Java https请求 HttpsURLConnection
- java 实现https请求
- java 实现https请求
- java Https的请求
- java 发送 https请求
- java 实现https请求
- Java https请求 HttpsURLConnection
- JAVA请求HTTPS协议
- java访问https请求
- java发送https请求
- JAVA请求HTTPS协议
- java 实现https请求
- java模拟https请求
- java模拟https请求
- java 实现https请求
- java 实现https请求
- java 发送HTTPS请求
- java https请求
- android 原生app嵌入reactnative页面
- 次导数 次梯度 小结
- ICLR'16 best paper 深度压缩,加速网络前传
- UNet实战系列(一)
- Kettle安装使用教程(未完待续)
- java https请求
- Android 高仿微信图片查看花式效果
- H2642PES
- UNet实战系列(二)
- LeetCode刷题(C++)——Odd Even Linked List(Medium)
- Android-程序、进程和线程
- 你知道吗?wb是默认开启兼容性视图的
- Linux 添加代理
- MapReduce编程练习(四),统计多个输入文件学生的平均成绩,