当 Android 看不到证书时,如何在 Android 中调用安全 (SSL) Web 服务?

发布于 2024-10-12 02:28:16 字数 569 浏览 6 评论 0原文

我是 Android 新手,正在努力为 Android 应用程序调用 SSL Web 服务。我的代码如下:

Log.v("fs", "Making HTTP call...");
HttpClient http = new DefaultHttpClient();
HttpGet request = new HttpGet("https://example.com/api");

try {

    String response = http.execute(request, new BasicResponseHandler());
    Log.v("fs", response);

} catch (Exception e) {

    Log.v("fs", e.toString());
}

输出是:

Making HTTP call...
javax.net.SSLPeerUnverifiedException: No peer certificate

任何使这项工作有效的建议都会很棒。

我应该指出,这是一个有效的证书。它由官方 CA 签署。

I'm new to Android and I'am struggling to make a call to an SSL web service for an Android Application. My code is as follows:

Log.v("fs", "Making HTTP call...");
HttpClient http = new DefaultHttpClient();
HttpGet request = new HttpGet("https://example.com/api");

try {

    String response = http.execute(request, new BasicResponseHandler());
    Log.v("fs", response);

} catch (Exception e) {

    Log.v("fs", e.toString());
}

The Output is:

Making HTTP call...
javax.net.SSLPeerUnverifiedException: No peer certificate

Any suggestions to make this work would be great.

I should note that this is a valid cert. It is signed by an official CA.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

〆一缕阳光ご 2024-10-19 02:28:16

您是否测试过手机日期和时间是否正确??,如果您使用 SSL 并且您的手机是 1990 年的,它将返回

javax.net.SSLPeerUnverifiedException:没有对等证书

问候

Have you test that the mobile date and hour are the correct ones ??, if you are using SSL and have your mobile in 1990 it will return

javax.net.SSLPeerUnverifiedException: No peer certificate

Regards

不必你懂 2024-10-19 02:28:16

尝试以下操作。我添加了 SSLSocketFactory 来处理 SSL 连接,此外它还添加了使用 ThreadSafeClientConnManager 同时处理多个连接的支持。您可以删除套接字超时和连接超时。

    SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
    schemeRegistry.register(new Scheme("mxiss", SSLSocketFactory.getSocketFactory(), 443));

    HttpParams params = new BasicHttpParams();
    int timeoutConnection = 5000;
    HttpConnectionParams.setConnectionTimeout(params, timeoutConnection);
    int timeoutSocket = 10000;
    HttpConnectionParams.setSoTimeout(params, timeoutSocket);
    params.setParameter(ConnManagerPNames.MAX_TOTAL_CONNECTIONS, 30);
    params.setParameter(ConnManagerPNames.MAX_CONNECTIONS_PER_ROUTE, new ConnPerRouteBean(30));
    params.setParameter(HttpProtocolParams.USE_EXPECT_CONTINUE, false);
    HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);

    ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(params, schemeRegistry);

    _client = new DefaultHttpClient(cm, params);

希望这有帮助。

如果您的客户端不信任服务器证书,请注册不同的协议并接受该协议的所有证书。在做任何事情之前你应该先注册协议。

Protocol mxiss = new Protocol("mxiss", new EasySSLProtocolSocketFactory(), 443);
Protocol.registerProtocol("mxiss", mxiss);

然后,您必须使用

来自 org.apache.commons.httpclient.contrib.ssl 的“mxiss”EasySSLProtocolSocketFactory,而不是“https”。将 jar 文件

Try the following. I have added SSLSocketFactory to handle SSL connections, plus it adds support for handling multiple connections simultaneously using ThreadSafeClientConnManager. You can remove socket timeout and connection timeouts.

    SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
    schemeRegistry.register(new Scheme("mxiss", SSLSocketFactory.getSocketFactory(), 443));

    HttpParams params = new BasicHttpParams();
    int timeoutConnection = 5000;
    HttpConnectionParams.setConnectionTimeout(params, timeoutConnection);
    int timeoutSocket = 10000;
    HttpConnectionParams.setSoTimeout(params, timeoutSocket);
    params.setParameter(ConnManagerPNames.MAX_TOTAL_CONNECTIONS, 30);
    params.setParameter(ConnManagerPNames.MAX_CONNECTIONS_PER_ROUTE, new ConnPerRouteBean(30));
    params.setParameter(HttpProtocolParams.USE_EXPECT_CONTINUE, false);
    HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);

    ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(params, schemeRegistry);

    _client = new DefaultHttpClient(cm, params);

Hope this helps.

If your client does not trust the server certificate register a different protocol and accept all certificates for that protocol. You should first register protocol before doing anything.

Protocol mxiss = new Protocol("mxiss", new EasySSLProtocolSocketFactory(), 443);
Protocol.registerProtocol("mxiss", mxiss);

Then instead of "https" you have to use "mxiss"

EasySSLProtocolSocketFactory comes from org.apache.commons.httpclient.contrib.ssl. Put the jar file http://repo1.maven.org/maven2/ca/juliusdavies/not-yet-commons-ssl/0.3.11/not-yet-commons-ssl-0.3.11.jar in your classpath.

送你一个梦 2024-10-19 02:28:16
SchemeRegistry schemeRegistry = new SchemeRegistry(); 
schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); 
schemeRegistry.register(new Scheme("mxiss", SSLSocketFactory.getSocketFactory(), 443));


HttpParams params = new BasicHttpParams(); 
int timeoutConnection = 5000; 
HttpConnectionParams.setConnectionTimeout(params, timeoutConnection); 
int timeoutSocket = 10000; 
HttpConnectionParams.setSoTimeout(params, timeoutSocket);

params.setParameter(ConnManagerPNames.MAX_TOTAL_CONNECTIONS, 30); 

params.setParameter(ConnManagerPNames.MAX_CONNECTIONS_PER_ROUTE, new ConnPerRouteBean); 

params.setParameter(HttpProtocolParams.USE_EXPECT_CONTINUE, false); 

HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);


ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(params, schemeRegistry);

_client = new DefaultHttpClient(cm, params);
SchemeRegistry schemeRegistry = new SchemeRegistry(); 
schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); 
schemeRegistry.register(new Scheme("mxiss", SSLSocketFactory.getSocketFactory(), 443));


HttpParams params = new BasicHttpParams(); 
int timeoutConnection = 5000; 
HttpConnectionParams.setConnectionTimeout(params, timeoutConnection); 
int timeoutSocket = 10000; 
HttpConnectionParams.setSoTimeout(params, timeoutSocket);

params.setParameter(ConnManagerPNames.MAX_TOTAL_CONNECTIONS, 30); 

params.setParameter(ConnManagerPNames.MAX_CONNECTIONS_PER_ROUTE, new ConnPerRouteBean); 

params.setParameter(HttpProtocolParams.USE_EXPECT_CONTINUE, false); 

HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);


ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(params, schemeRegistry);

_client = new DefaultHttpClient(cm, params);
凑诗 2024-10-19 02:28:16

我也认为这是服务器证书的问题。请查看此处给出的InstallCert.java程序: http://blogs.oracle.com/andreas/条目/no_more_unable_to_find。这是验证服务器证书的好方法。如果您无法使用此程序下载该程序,我想说,请再次验证您的服务器。
在此处发布使用该程序后的结果,我们将从那里获取结果。

I too think this is an issue with server's certificate. Pls check out the InstallCert.java program given here: http://blogs.oracle.com/andreas/entry/no_more_unable_to_find. This can be a good way to verify server's certificate. If you can't download that using this program, I would say, verify your server again.
Post your results after using this program here, and we will take it from there.

浪漫人生路 2024-10-19 02:28:16

我假设您无法获取 HTTPS Web 服务。唯一允许的方法是 POST 尝试看看。

i assume you cannot have Get in HTTPS webservice. Only Method allowed is POST try that and see.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文