使用自定义http客户端无法连接到Https端点 - javax.net.ssl.SSLPeerUnverifiedException:无对等证书

时间:2018-03-12 13:31:40

标签: android https

请为这个问题搜索所有可用的解决方案,但似乎没有一个对我有用。我使用HTTPS连接到端点,但它无法正常工作。它适用于较高版本的Android,例如7.0但不适用于4.4等较低版本。我尝试连接之前,网站从http升级到https,一切似乎都很好。但是,现在使用https,设备与较低版本的android aint连接。即使使用自定义http客户端,也没有成功。

请在这里找到我的连接代码

这是自定义http客户端:

public static HttpClient createHttpClient()
{
    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.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);

    return new DefaultHttpClient(conMgr, params);
}catch (Exception e){
        Log.e(TAG, "SSL catch exception");

        return new DefaultHttpClient();
    }
}

这是实施:

 public AuthorizationHttpResponse makeServiceCallJSONSSL(String url, int 
 method, JSONObject params)
{


    AuthorizationHttpResponse httpResponses = new AuthorizationHttpResponse();
    try {
        // http client
        DefaultHttpClient httpClient = new DefaultHttpClient();

        //ssl
        HttpClient httpclient = createHttpClient();


        HttpEntity httpEntity = null;
        HttpResponse httpResponse = null;
        // Checking http request method type
        if (method == POST) {
            Log.e("in POST","in POST");
            HttpPost httpPost = new HttpPost(url);
            //HttpPost httppost = new HttpPost(" URL");

            // adding post params
            if (params != null) {
                Log.e("in POST params","in POST params");

                httpPost.setHeader("Content-type", "application/json");
                httpPost.setHeader("Accept", "application/json");
                //httpPost.setEntity(new UrlEncodedFormEntity(params));
                httpPost.setEntity(new StringEntity(params.toString(), "UTF-8"));
            }
            Log.e("url in post service",url);
            httpResponse = httpclient.execute(httpPost);

            //HttpClient httpclient = createHttpClient();
            //HttpPost httpResp = new HttpPost(url);


        } else if (method == GET) {
            // appending params to url
            Log.e("in GET","in GET");
            if (params != null) {
                Log.e("in GET params","in GET params");
                //cannot use raw json for get method
                //  String paramString = URLEncodedUtils
                //       .format(params, "utf-8");
                //url += "?" + paramString;
            }
            Log.e("url in get service", url);

            HttpGet httpGet = new HttpGet(url);

            httpResponse = httpClient.execute(httpGet);



        }
        httpEntity = httpResponse.getEntity();
        response = EntityUtils.toString(httpEntity);
        responseCode = httpResponse.getStatusLine().getStatusCode();
        httpResponses.setResponseData(response);
        httpResponses.setResponseCode(responseCode);

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    Log.e(TAG, "Response code from server : " + responseCode + ". response gotten from service:" + response);
    return httpResponses;
}

0 个答案:

没有答案