java 的 代码访问https JDk环境的问题

DoreenWang 发布于 2016/06/06 18:11
阅读 310
收藏 0

不知道为什么同样的代码,环境用jdk 1.6.0_43 的时候就一直报错,但是用jdk1.7的就没有错,不论32位还是64位的都不会错,

public static void main(String[] args) {

HttpClient HttpClient = getSSLHttpClient();

String url = "https://www.baidu.com/";
String x = getDoc(HttpClient, url);
System.out.println(x);
}

public static String getDoc(HttpClient httpClient, String url) {
HttpGet get = null;
try {

get = new HttpGet(url);
get.setHeader(
"User-Agent",
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.87 Safari/537.36");
// 执行getMethod
HttpResponse response = httpClient.execute(get);
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_OK) {
System.err.println("Method failed: "
+ response.getStatusLine().getStatusCode());
}
// 读取内容
InputStream inputStream = response.getEntity().getContent();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
IOUtils.copy(inputStream, baos);
try {
String result = new String(baos.toByteArray(), "utf-8");
return result;
} catch (Exception e) {

}
return "";
} catch (Exception e) {
e.printStackTrace();
} finally {
// 释放连接
if (null != get)
get.abort();
}
return "";
}

public static HttpClient getSSLHttpClient() {
HttpClient httpClient = new DefaultHttpClient();

X509TrustManager xtm = new X509TrustManager() { // 创建TrustManager
public void checkClientTrusted(X509Certificate[] chain,
String authType) throws CertificateException {
}

public void checkServerTrusted(X509Certificate[] chain,
String authType) throws CertificateException {
}

public X509Certificate[] getAcceptedIssuers() {
return null;
}
};

try {
// TLS1.0与SSL3.0基本上没有太大的差别,可粗略理解为TLS是SSL的继承者,但它们使用的是相同的SSLContext
SSLContext ctx = SSLContext.getInstance("TLS");

// 使用TrustManager来初始化该上下文,TrustManager只是被SSL的Socket所使用
ctx.init(null, new TrustManager[] { xtm }, null);

// 创建SSLSocketFactory
SSLSocketFactory socketFactory = new SSLSocketFactory(ctx);

// 通过SchemeRegistry将SSLSocketFactory注册到我们的HttpClient上
httpClient.getConnectionManager().getSchemeRegistry()
.register(new Scheme("https", 443, socketFactory));
} catch (KeyManagementException e1) {
// TODO
e1.printStackTrace();

} catch (NoSuchAlgorithmException e1) {
// TODO
e1.printStackTrace();

}

httpClient.getParams().setParameter(
CoreConnectionPNames.CONNECTION_TIMEOUT, 20000);
httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT,
20000);
return httpClient;
}
上面是代码

如果环境用jdk 1.6.0_43  就会报错,报下面这个错

javax.net.ssl.SSLKeyException: RSA premaster secret error
at com.sun.net.ssl.internal.ssl.RSAClientKeyExchange.<init>(Unknown Source)
at com.sun.net.ssl.internal.ssl.ClientHandshaker.serverHelloDone(Unknown Source)
at com.sun.net.ssl.internal.ssl.ClientHandshaker.processMessage(Unknown Source)
at com.sun.net.ssl.internal.ssl.Handshaker.processLoop(Unknown Source)
at com.sun.net.ssl.internal.ssl.Handshaker.process_record(Unknown Source)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readRecord(Unknown Source)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(Unknown Source)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(Unknown Source)
at org.apache.http.conn.ssl.SSLSocketFactory.connectSocket(SSLSocketFactory.java:535)
at org.apache.http.conn.ssl.SSLSocketFactory.connectSocket(SSLSocketFactory.java:403)
at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:177)
at org.apache.http.impl.conn.ManagedClientConnectionImpl.open(ManagedClientConnectionImpl.java:304)
at org.apache.http.impl.client.DefaultRequestDirector.tryConnect(DefaultRequestDirector.java:611)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:446)
at org.apache.http.impl.client.AbstractHttpClient.doExecute(AbstractHttpClient.java:863)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:106)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:57)
at SSL.SSLCommon12.getDoc(SSLCommon12.java:66)
at SSL.SSLCommon12.main(SSLCommon12.java:53)
Caused by: java.security.NoSuchAlgorithmException: SunTlsRsaPremasterSecret KeyGenerator not available
at javax.crypto.KeyGenerator.<init>(DashoA13*..)
at javax.crypto.KeyGenerator.getInstance(DashoA13*..)
at com.sun.net.ssl.internal.ssl.JsseJce.getKeyGenerator(Unknown Source)
... 21 more

加载中
返回顶部
顶部