java https 请求
2016-07-18 10:20
549 查看
java 可以直接通过以下代码访问 https url:
URL myURL = new URL("https://joloapi.com/");
HttpsURLConnection httpsConn = (HttpsURLConnection) myURL.openConnection();
// 单位是 ms,设置为 30 s
httpsConn.setConnectTimeout(30000);
httpsConn.setReadTimeout(30000);
InputStreamReader insr = new InputStreamReader(httpsConn.getInputStream());
int respInt = insr.read();
while (respInt != -1) {
System.out.print((char) respInt);
respInt = insr.read();
}
但是对于部分 https url 会报错,比如 joloapi.com 会报
解决方法是:
从 http://www.bouncycastle.org/latest_releases.html 下载两个文件:
bcprov-ext-jdk15on-154.jar 和 bcprov-jdk15on-154.jar
添加到 JAVA_HOME/jre/lib/ext 目录;修改 jre/lib/security/java.security 文件,添加
security.provider.10=org.bouncycastle.jce.provider.BouncyCastleProvider
另 java 访问 https 时默认会检查证书,如果要访问比如 12306 这样的网站,需要从网站下载证书并导入到 java 的信任证书,这个相关文章比较多,就不详细说了
URL myURL = new URL("https://joloapi.com/");
HttpsURLConnection httpsConn = (HttpsURLConnection) myURL.openConnection();
// 单位是 ms,设置为 30 s
httpsConn.setConnectTimeout(30000);
httpsConn.setReadTimeout(30000);
InputStreamReader insr = new InputStreamReader(httpsConn.getInputStream());
int respInt = insr.read();
while (respInt != -1) {
System.out.print((char) respInt);
respInt = insr.read();
}
但是对于部分 https url 会报错,比如 joloapi.com 会报
"Could not generate DH keypair"
解决方法是:从 http://www.bouncycastle.org/latest_releases.html 下载两个文件:
bcprov-ext-jdk15on-154.jar 和 bcprov-jdk15on-154.jar
添加到 JAVA_HOME/jre/lib/ext 目录;修改 jre/lib/security/java.security 文件,添加
security.provider.10=org.bouncycastle.jce.provider.BouncyCastleProvider
另 java 访问 https 时默认会检查证书,如果要访问比如 12306 这样的网站,需要从网站下载证书并导入到 java 的信任证书,这个相关文章比较多,就不详细说了
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- PropertyChangeListener简单理解
- c++11 + SDL2 + ffmpeg +OpenAL + java = Android播放器
- 插入排序
- 冒泡排序
- 堆排序
- 快速排序