先确认要访问的网站支持什么协议
https://www.ssllabs.com/ssltest
要访问的网站只支持TLS1.2
可以看出jdk6不支持TLS1.2
要自己实现TLS1.2的访问,代码参考:
https://blog.csdn.net/qq_32016029/article/details/80222013
需要JAR包
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15on</artifactId>
<version>1.54</version>
</dependency>
下载的时候发现maven竟然不支持http访问了,通过pom.xml竟然下载不到
还好在maven的报错信息里有一个地址可以下载
https://repo.maven.apache.org/maven2/org/bouncycastle/bcprov-jdk15on/1.60/
进去自己找jar包下载
jar包和代码都get后,发现网站只支持get访问
HttpsUrlConnectionForTLS httpsUrlConnectionMessageSender = new HttpsUrlConnectionForTLS();
String url = "https://";
HttpURLConnection connection = null;
try
{
connection = httpsUrlConnectionMessageSender.createConnection(new URI(url ));
connection.setRequestMethod("GET");
connection.setConnectTimeout(15000);
connection.setReadTimeout(60000);
connection.connect();
String str = HttpsUrlConnectionForTLS.getResponse(connection);
//System.out.println(str);
return str;
}
catch (Exception e)
{
throw e;
}
finally
{
if (connection != null)
{
connection.disconnect();
}
}
成功
参考资料:
https://www.cnblogs.com/yangmengdx3/p/6322228.html
https://blog.csdn.net/sun5769675/article/details/81322324