https用于绕过证书不对情况
import java.net.MalformedURLException;
import java.net.URL;
import java.security.cert.Certificate;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLPeerUnverifiedException;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocketFactory;
import com.sun.net.ssl.internal.ssl.SSLSocketFactoryImpl;
import sun.net.www.protocol.https.Handler;
public class TestHqm {
public static void main(String[] args) throws Exception {
String remoteURL = "https://member.test.com/efg/abc/receiveOrder.do" ;
SSLSocketFactoryImpl socketFactoryImp = null;
SSLSocketFactory ssf= BZX509TrustManager.getSSFactory();
URL theURL = new URL(null, remoteURL, new Handler());
HttpsURLConnection connection = (HttpsURLConnection) theURL.openConnection();
connection.setSSLSocketFactory(ssf);
//System.out.println(socketFactoryImp);
System.out.println("=================================================");
//HostnameVerifier hostNamever = connection.getDefaultHostnameVerifier();
//connection.setHostnameVerifier(new MyHostnameVerifier());
//System.out.println(connection.getCipherSuite());
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
connection.setDefaultUseCaches(false);
connection.setRequestMethod("POST");
connection.connect();
System.out.println("111");
}
private static class MyHostnameVerifier implements HostnameVerifier {
public boolean verify(String hostname, SSLSession session) {
try {
Certificate[] certs = session.getPeerCertificates();
for (Certificate cer : certs) {
System.out.println(cer.toString());
}
} catch (SSLPeerUnverifiedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//return hostname.equals(session.getPeerHost());
return true;
}
}
}
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
public class BZX509TrustManager implements X509TrustManager {
public BZX509TrustManager(){}
@Override
public void checkClientTrusted(X509Certificate[] arg0, String arg1)
throws CertificateException {
// TODO Auto-generated method stub
System.out.println("checkClientTrusted");
}
@Override
public void checkServerTrusted(X509Certificate[] arg0, String arg1)
throws CertificateException {
System.out.println(arg0[0]);
}
@Override
public X509Certificate[] getAcceptedIssuers() {
// TODO Auto-generated method stub
return null;
}
public static SSLSocketFactory getSSFactory() throws NoSuchAlgorithmException, NoSuchProviderException, KeyManagementException{
TrustManager[] tm = { new BZX509TrustManager()};
SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE");
sslContext.init(null, tm, new java.security.SecureRandom());
SSLSocketFactory ssf = sslContext.getSocketFactory();
return ssf;
}
}