package httpssl;
import java.io.InputStreamReader;
import java.net.URL;
import java.security.Security;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
/**
*
* @author quansheng
*/
public class T2 {
public static void main(String[] args) throws Exception {
HostnameVerifier hv = new HostnameVerifier() {
public boolean verify(String string, SSLSession ssls) {
return true;
}
};
trustAllHttpsCertificates();
HttpsURLConnection.setDefaultHostnameVerifier(hv);
URL myURL = new URL("https://211.100.34.105/picring/xxxx");
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
HttpsURLConnection httpsConn = (HttpsURLConnection) myURL.openConnection();
InputStreamReader insr = new InputStreamReader(httpsConn.getInputStream());
int respInt = insr.read();
while (respInt != -1) {
System.out.print((char) respInt);
respInt = insr.read();
}
}
private static void trustAllHttpsCertificates() throws Exception {
TrustManager[] trustAllCerts = new javax.net.ssl.TrustManager[]{
new javax.net.ssl.X509TrustManager() {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType) {
}
public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType) {
}
}
};
javax.net.ssl.SSLContext sc =
javax.net.ssl.SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, null);
javax.net.ssl.HttpsURLConnection.setDefaultSSLSocketFactory(
sc.getSocketFactory());
}
}
java程序访问不可信(自颁发证书)ssl http 资源
最新推荐文章于 2023-03-14 21:33:59 发布