Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
695 views
in Technique[技术] by (71.8m points)

android - How to create an https Connection?

My code works for http, but not https. I'm trying to make Https connections on the Android phones, using HttpClient. Trouble is that i keep getting net.ssl.SSLPeerUnverifiedException: No peer certificate. and Caused by: java.security.cert.CertificateException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.

Here is the relavent code for the Custom 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;
}

this is the code for getting information from the server

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

Any thoughts?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Finally i got the solution to my Problem, for this solution i searched a lot in three days. Problem with the authentication of the server with the username and password. I changed code like this. i am passing credentials to the server that is only change in my code other than code available in the Question..

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...