Android 4.4中没有可用的对等证书 - 代码适用于Android 6

时间:2017-06-26 05:46:44

标签: android ssl apache-httpclient-4.x

我正在努力解决调用POST URL以获取oAuth令牌的问题。我正在使用HTTPClient到URL来发布获取oauth访问令牌的请求。它在Android 6及更高版本中运行良好。在android 5或更低版本中尝试相同时,会给我No Peer证书可用异常。下面是代码,我在Stackoverflow中尝试了很多答案并且无法解决我的问题:(。

    httpClient = new DefaultHttpClient();
    httpPost = new HttpPost(address);


    params.add(new BasicNameValuePair("code", token));
    params.add(new BasicNameValuePair("redirect_uri", ""));
    params.add(new BasicNameValuePair("grant_type", ""));

    String encoding = Base64.encodeToString("".getBytes(), Base64.DEFAULT);
    //httpPost.setHeader("Authorization", "Basic =");

    String auth = "" + ":" + "";
    byte[] encodedAuth = org.apache.commons.codec.binary.Base64.encodeBase64(auth.getBytes(Charset.forName("ISO-8859-1")));
    String authHeader = "Basic " + new String(encodedAuth);
    httpPost.setHeader("Authorization", authHeader);


    httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");
    httpPost.setEntity(new UrlEncodedFormEntity(params));

    //httpPost.setParams(params);
    HttpResponse httpResponse = httpClient.execute(httpPost);
    HttpEntity httpEntity = httpResponse.getEntity();

    is = httpEntity.getContent();

    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    StringBuilder out = new StringBuilder();
    String line;
    while ((line = reader.readLine()) != null) {
        out.append(line);
    }

    reader.close();

0 个答案:

没有答案