我的代码适用于http,但不适用于https。
我正在尝试使用HttpClient在Android手机上制作Https connections。问题是我一直在获取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();
    }
}


有什么想法吗?

最佳答案

最终,我找到了解决问题的方法,为此,我在三天内进行了很多搜索。使用用户名和密码验证服务器的问题。我改变了这样的代码。我正在将凭据传递给服务器,该凭据仅在我的代码中有所更改,而不是问题中可用的代码。

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();
    }
}

关于android - 如何创建一个https连接?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12136907/

10-12 00:30
查看更多