无证书访问https接口
调用https接口
MySecureProtocolSocketFactory.java
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.SecureProtocolSocketFactory;
public class MySecureProtocolSocketFactory implements 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 (this.sslcontext == null) {
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[]{};
}
}
}
HttpsUtil.java (现在大家可以测试喽~~~~)
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Iterator;
import java.util.Map;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.protocol.Protocol;
import org.apache.log4j.Logger;
public class HttpsUtil {
private static Logger log = Logger.getLogger(HttpsUtil.class);
private static Protocol https = null;
static {
https = new Protocol("https", new MySecureProtocolSocketFactory(), 443);
Protocol.registerProtocol("https", https);
}
public static String sendByGet(String url){
HttpClient httpclient = null;
GetMethod method = null;
String result = "";
try {
httpclient = new HttpClient();
method = new GetMethod(url);
httpclient.executeMethod(method);
//result = method.getResponseBodyAsString();
BufferedReader bs = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream(), "utf-8"));
String line = "";
while((line = bs.readLine()) != null){
result += line;
}
log.info("HttpsUtil-sendByGet-url=" + url + "-result=" + result);
} catch(Exception ex){
ex.printStackTrace();
result = "error\tcrm error " + ex.toString();
log.error("HttpsUtil-sendByGet-url=" + url + "-result=" + ex.toString());
} finally{
if(method != null){
method.releaseConnection();
}
}
return result;
}
public static String sendByPost(String url, Map param){
HttpClient httpclient = null;
PostMethod method = null;
String result = "";
try {
httpclient = new HttpClient();
method = new PostMethod(url);
if(param != null && !param.isEmpty()){
int len = param.size();
method.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=utf-8");
NameValuePair[] np = new NameValuePair[len];
Iterator iter = param.keySet().iterator();
int i = 0;
while(iter.hasNext()){
String k = (String)iter.next();
String v = (String)param.get(k) ;
np[i].setName(k);
np[i].setValue(v);
}
method.setRequestBody(np);
}
httpclient.executeMethod(method);
//result = method.getResponseBodyAsString();
BufferedReader bs = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream(), "utf-8"));
String line = "";
while((line = bs.readLine()) != null){
result += line;
}
log.info("HttpsUtil-sendByPost-url=" + url + "-result=" + result);
} catch(Exception ex){
ex.printStackTrace();
result = "error\tcrm error " + ex.toString();
log.error("HttpsUtil-sendByPost-url=" + url + "-result=" + ex.toString());
} finally{
if(method != null){
method.releaseConnection();
}
}
return result;
}
}