1

我的代码适用于 http,但不适用于 https。我正在尝试Https connections使用 HttpClient 在 Android 手机上制作。麻烦的是我不断得到net.ssl.SSLPeerUnverifiedException: No peer certificate。和Caused by: java.security.cert.CertificateException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.

这是自定义 HttpClient 的相关代码。

public static HttpClient getNewHttpClient() {

     HttpParams params = new BasicHttpParams();
     HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
     HttpProtocolParams.setContentCharset(params, HTTP.DEFAULT_CONTENT_CHARSET);
     HttpProtocolParams.setUseExpectContinue(params, true);

     SchemeRegistry schReg = new SchemeRegistry();
     schReg.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
     schReg.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
     ClientConnectionManager conMgr = new ThreadSafeClientConnManager(params, schReg);

     DefaultHttpClient http = new DefaultHttpClient(conMgr, params);
     UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("name", "pass");
     AuthScope authScope = new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT);
     http.getCredentialsProvider().setCredentials(authScope, credentials);

     return http;
}

这是从服务器获取信息的代码

public static void Get() {

    HttpClient http = getNewHttpClient();    

    HttpResponse response;
    try {
        HttpGet httpost = new HttpGet(url);
        response = http.execute(httpost);
        BufferedReader in = null;
        Log.i(TAG, "resp-" + response);
        in = new BufferedReader(new InputStreamReader(response.getEntity()
                .getContent()));
        StringBuffer sb = new StringBuffer("");
        String line = "";
        String NL = System.getProperty("line.separator");
        while ((line = in.readLine()) != null) {
            sb.append(line + NL);
        }
        in.close();
        String page = sb.toString();
        Log.i(TAG, "res=" + sb.toString());
        tv.setText(page);
    } catch (ClientProtocolException e) {
        Log.i(TAG, "ClientProtocolException=" + e);
        e.printStackTrace();
    } catch (IOException e) {
        Log.i(TAG, "ClientProtocolException=" + e);
        e.printStackTrace();
    }
} 

有什么想法吗?

4

2 回答 2

2

最后我得到了我的问题的解决方案,对于这个解决方案,我在三天内搜索了很多。使用用户名和密码对服务器进行身份验证时出现问题。我改变了这样的代码。我正在将凭据传递给服务器,该凭据仅在我的代码中发生更改,而不是问题中可用的代码..

public static HttpClient _getNewHttpClient() {
    try {
        KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
        trustStore.load(null, null);

        SSLSocketFactory sf = new MySSLSocketFactory(trustStore);
        sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

        HttpParams params = new BasicHttpParams();
        HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
        HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);

        SchemeRegistry registry = new SchemeRegistry();
        registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
        registry.register(new Scheme("https", sf, 443));

        ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry);

        DefaultHttpClient http = new DefaultHttpClient(ccm, params);
        UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("jk", "jk");
        AuthScope authScope = new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT);
        http.getCredentialsProvider().setCredentials(authScope, credentials);

        return http;
    } catch (Exception e) {
        return new DefaultHttpClient();
    }
}   
于 2012-08-29T06:25:35.873 回答
-1

尝试在这篇文章的答案中查看 https over HttpClient 是如何实现的:Trusting all certificate using HttpClient over HTTPS

他们已经详细讨论过 Keystore 和 TrustManager

于 2012-08-28T05:11:43.557 回答