不显示删除回复显示所有回复显示星级回复显示得分回复 关于httpclient带证书访问https地址,求高手急救....

jales123 2010-10-21 10:14:45
程序访问https协议的接口地址,我采用第一种方式:
从浏览器保存下来对应https地址的的证书,然后向$JAVA_HOME/jre/lib/security/cacerts文件中导入,然后使用httpclient程序(像访问http一样的程序)去访问https,一切正常。
然而我采用第二种方式:
从浏览器保存下来对应https地址的的证书, 然后使用
keytool -import -noprompt -keystore msg.keystore -storepass 123456 -alias example -file e:/test.cer
生成一个 msg.keystore, 然后用如下程序去 访问https地址:
String requestp = "abcdefg";

DefaultHttpClient httpclient = new DefaultHttpClient();

KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
FileInputStream instream = new FileInputStream(new File("c:\\msg.keystore"));
try {
trustStore.load(instream, "123456".toCharArray());
} finally {
instream.close();
}
SSLSocketFactory socketFactory = new SSLSocketFactory(trustStore);
Scheme sch = new Scheme("https", socketFactory, 443);
httpclient.getConnectionManager().getSchemeRegistry().register(sch);

HttpPost hp = new HttpPost("https://11.12.13.14:8443/platform");
hp.setHeader("Content-Type", "application/xml");
hp.setHeader("Accept", "application/xml");
HttpEntity entity = new ByteArrayEntity(requestp.getBytes());
hp.setEntity(entity);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String response = httpclient.execute(hp,responseHandler);

System.out.println("----------------------------------------");
System.out.println(response);
httpclient.getConnectionManager().shutdown();

最后却报出了如下异常:
Exception in thread "main" javax.net.ssl.SSLException: hostname in certificate didn't match: <11.12.13.14> != <example>
at org.apache.http.conn.ssl.AbstractVerifier.verify(AbstractVerifier.java:220)
at org.apache.http.conn.ssl.BrowserCompatHostnameVerifier.verify(BrowserCompatHostnameVerifier.java:54)
at org.apache.http.conn.ssl.AbstractVerifier.verify(AbstractVerifier.java:149)
at org.apache.http.conn.ssl.AbstractVerifier.verify(AbstractVerifier.java:130)
at org.apache.http.conn.ssl.SSLSocketFactory.connectSocket(SSLSocketFactory.java:339)
at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:123)
at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:147)
at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:101)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:381)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:641)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:730)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:708)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:699)
at interfaces.Test.main(Test.java:52)
小弟实在费解,请高人赐教 ,不胜感激!!!
...全文
59 点赞 收藏 回复
写回复
回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
相关推荐
发帖
Web 开发
创建于2007-09-28

8.0w+

社区成员

Java Web 开发
申请成为版主
帖子事件
创建了帖子
2010-10-21 10:14
社区公告
暂无公告