HttpClient访问https网站的问题
我想用HttpClient访问一个https网站,我已经拥有服务端证书和客户端证书,我将服务端证号和客户端证号都导入到jdk的cacerts中,也按着网上的办法:
httpclient完全支持ssl连接方式。通常,如果不需要进行客户端认证和服务器端认证的ssl连接,httpclient的处理方式是和http方式完全一样。
现在这里是讲的是需要客户端认证数字证书时的httpclient处理方式(因为需要客户端认证时,连接会被主动关闭)。
1。使用ie访问你要连结的url地址,这时你会看到弹出一个询问是否继续和服务器建立连接的对话框(安全警报)。选择“查看证书”->“详细信息”->“复制文件到”导出数字证书(例: server.cer或server.crt)。
2。使用导出的数字证书来创建你的keystore
keytool -import -alias "my server cert" -file server.cer -keystore my.truststore
keytool -genkey -v -alias "my client key" -validity 365 -keystore my.keystore
3。在引入AuthSSLProtocolSocketFactory.java,AuthSSLX509TrustManager.java和AuthSSLInitializationError后在你的代码里按下面的例子里来进行ssl连接
Protocol authhttps = new Protocol("https",
new AuthSSLProtocolSocketFactory(
new URL("file:my.keystore"), "mypassword",
new URL("file:my.truststore"), "mypassword"), 8443);
HttpClient client = new HttpClient();
client.getHostConfiguration().setHost("sh.12530", 8443, authhttps);
/*只能使用相对路径*/
GetMethod httpget = new GetMethod("/");
client.executeMethod(httpget);
执行后报错:
javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure
我确认程序是已经找到服务端证书了,可能在客户端认证的时候出错了,请问我错在哪了,怎么办?
httpclient完全支持ssl连接方式。通常,如果不需要进行客户端认证和服务器端认证的ssl连接,httpclient的处理方式是和http方式完全一样。
现在这里是讲的是需要客户端认证数字证书时的httpclient处理方式(因为需要客户端认证时,连接会被主动关闭)。
1。使用ie访问你要连结的url地址,这时你会看到弹出一个询问是否继续和服务器建立连接的对话框(安全警报)。选择“查看证书”->“详细信息”->“复制文件到”导出数字证书(例: server.cer或server.crt)。
2。使用导出的数字证书来创建你的keystore
keytool -import -alias "my server cert" -file server.cer -keystore my.truststore
keytool -genkey -v -alias "my client key" -validity 365 -keystore my.keystore
3。在引入AuthSSLProtocolSocketFactory.java,AuthSSLX509TrustManager.java和AuthSSLInitializationError后在你的代码里按下面的例子里来进行ssl连接
Protocol authhttps = new Protocol("https",
new AuthSSLProtocolSocketFactory(
new URL("file:my.keystore"), "mypassword",
new URL("file:my.truststore"), "mypassword"), 8443);
HttpClient client = new HttpClient();
client.getHostConfiguration().setHost("sh.12530", 8443, authhttps);
/*只能使用相对路径*/
GetMethod httpget = new GetMethod("/");
client.executeMethod(httpget);
执行后报错:
javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure
我确认程序是已经找到服务端证书了,可能在客户端认证的时候出错了,请问我错在哪了,怎么办?
...全文
相关推荐
基于HttpClient 4.3的可访问自签名HTTPS站点的新版工具类
本文将深入探讨基于HttpClient 4.3的工具类,尤其是如何处理自签名HTTPS站点的访问问题。首先,让我们理解什么是自签名HTTPS证书。
Spring Boot RestTemplate 忽略证书访问https
这里介绍如何在Spring Boot中信任所有的站点,在不导入证书的状况下直接可以访问https站点。
HttpClient和HttpsUrlConnection
在Android中,访问HTTPS网站时,系统会自动检查服务器的数字证书,确保其由受信任的证书颁发机构签名,并且未过期。如果服务器的证书出现问题,系统会抛出安全异常,阻止连接。
java httpclient 模拟登录
在"java httpclient 模拟登录"这个场景下,我们通常会用到HttpClient来模拟用户登录网站的过程,获取登录后的session信息,以便后续能够访问登录后才能看到的页面内容。
httpclient 初步
**下载HttpClient库** - 访问Apache官方网站(http://jakarta.apache.org/commons/httpclient/downloads.html)下载HttpClient


Web 开发
