验证自签名安全证书,Android
使用 HttpClient
连接到 .NET Web 服务时,我需要验证自签名证书。任何人都可以将我重定向到任何资源吗?
谢谢,
泰贾。
编辑:根据我在发布后了解到的内容,以下 src 代码来自 http://svn.apache.org/repos/asf/httpcomponents/httpclient/branches/4.0.x/httpclient/src/ example/org/apache/http/examples/client/ClientCustomSSL.java 应该有所帮助 -
public final static void main(String[] args) throws Exception {
DefaultHttpClient httpclient = new DefaultHttpClient();
KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
FileInputStream instream = new FileInputStream(new File("my.keystore"));
try {
trustStore.load(instream, "nopassword".toCharArray());
} finally {
instream.close();
}
SSLSocketFactory socketFactory = new SSLSocketFactory(trustStore);
Scheme sch = new Scheme("https", socketFactory, 443);
httpclient.getConnectionManager().getSchemeRegistry().register(sch);
HttpGet httpget = new HttpGet("https://localhost/");
System.out.println("executing request" + httpget.getRequestLine());
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
if (entity != null) {
System.out.println("Response content length: " + entity.getContentLength());
}
if (entity != null) {
entity.consumeContent();
}
// When HttpClient instance is no longer needed,
// shut down the connection manager to ensure
// immediate deallocation of all system resources
httpclient.getConnectionManager().shutdown();
}
现在我的麻烦是生成 my.keystore 文件并为其创建密码。有什么建议吗?
谢谢, 特贾。
edit2:更多更新。尝试在 mac 上使用 keytool 将其导出到 .keystore,但不知道该怎么做。它一直说“鉴于最后一个块没有正确填充”。
为什么它让我跳过这么多的圈子? :@ 这对 B'Berry 和 Droid 来说太简单了 :(
I need to validate a self-signed certificate when connecting to a .NET web service using HttpClient
. Can anyone redirect me to any resources ?
Thanks,
Teja.
Edit: From what I've learned after posting, the following src code from http://svn.apache.org/repos/asf/httpcomponents/httpclient/branches/4.0.x/httpclient/src/examples/org/apache/http/examples/client/ClientCustomSSL.java should help -
public final static void main(String[] args) throws Exception {
DefaultHttpClient httpclient = new DefaultHttpClient();
KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
FileInputStream instream = new FileInputStream(new File("my.keystore"));
try {
trustStore.load(instream, "nopassword".toCharArray());
} finally {
instream.close();
}
SSLSocketFactory socketFactory = new SSLSocketFactory(trustStore);
Scheme sch = new Scheme("https", socketFactory, 443);
httpclient.getConnectionManager().getSchemeRegistry().register(sch);
HttpGet httpget = new HttpGet("https://localhost/");
System.out.println("executing request" + httpget.getRequestLine());
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
if (entity != null) {
System.out.println("Response content length: " + entity.getContentLength());
}
if (entity != null) {
entity.consumeContent();
}
// When HttpClient instance is no longer needed,
// shut down the connection manager to ensure
// immediate deallocation of all system resources
httpclient.getConnectionManager().shutdown();
}
Now my trouble is generating the my.keystore file and creating a password for it. Any tips?
Thanks,
Teja.
edit2: More updates. Tried using keytool on mac to export it to a .keystore, couldn't figure out how to do it. It keeps saying "Given final block not properly padded".
Why is it making me jump through so many hoops? :@ This was so easy on the B'Berry and the Droid :(
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论