How to create an https Connection?

后端 未结 2 2001
北恋
北恋 2020-12-18 16:01

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.s

相关标签:
2条回答
  • 2020-12-18 16:41

    Try looking at how https over HttpClient was implemented in the answers in this post: Trusting all certificates using HttpClient over HTTPS

    They have talked in detail on Keystore and TrustManager

    0 讨论(0)
  • 2020-12-18 16:49

    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();
        }
    }   
    
    0 讨论(0)
提交回复
热议问题