如何建立HTTPS连接? [英] How to create an https Connection?

查看:161
本文介绍了如何建立HTTPS连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的code适用于HTTP,而不是HTTPS。 我试图让的Https的Andr​​oid手机连接,使用HttpClient的。麻烦的是,我不断收到 net.ssl​​.SSLPeerUnverifiedException:没有对方的证书。和产生的原因:java.security.cert.CertificateException:java.security.cert.CertPathValidatorException:找不到证书路径信任锚

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.

下面是relavent code为自定义的HttpClient。

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

这是在code从服务器

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

有什么想法?

推荐答案

我终于得到了解决我的问题,这个解决方案,我在三天内搜索了很多。问题与服务器与用户名和密码的验证。我改变code这样。我传递凭据是在我的code以外code可用的问题的唯一变化的服务器。

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

这篇关于如何建立HTTPS连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆