Android的HTTPS POST无法在较新设备(Android HTTPs post fails

2019-10-20 05:32发布

前几天我升级我的应用程序引擎服务器的安全的HTTP请求只(HTTPS)的工作。 (通过添加“安全:总是”在app.yaml文件行)。

一切运行良好,我设法得到我的应用我的应用程序引擎服务器响应(在Android 4.1.2运行),但今天我发现,在4.4.2的设备,我得到以下错误:

06-04 20:11:29.501: W/System.err(21158): javax.net.ssl.SSLPeerUnverifiedException: No peer certificate
06-04 20:11:29.504: W/System.err(21158):    at com.android.org.conscrypt.SSLSessionImpl.getPeerCertificates(SSLSessionImpl.java:146)
06-04 20:11:29.505: W/System.err(21158):    at org.apache.http.conn.ssl.AbstractVerifier.verify(AbstractVerifier.java:93)
06-04 20:11:29.505: W/System.err(21158):    at org.apache.http.conn.ssl.SSLSocketFactory.createSocket(SSLSocketFactory.java:388)
06-04 20:11:29.505: W/System.err(21158):    at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:165)
06-04 20:11:29.506: W/System.err(21158):    at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
06-04 20:11:29.506: W/System.err(21158):    at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
06-04 20:11:29.506: W/System.err(21158):    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
06-04 20:11:29.507: W/System.err(21158):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
06-04 20:11:29.507: W/System.err(21158):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
06-04 20:11:29.507: W/System.err(21158):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)

这是负责发送与HTTP POST请求一个JSON的客户端的代码:

static private String getJson(String json,String url){
    HttpClient httpClient = new DefaultHttpClient();

    String responseString="";
    try {
        HttpPost request = new HttpPost("https://XXXXXX.appspot.com/XXXX/XXXX");
        StringEntity params =new StringEntity(json, "UTF-8");
        request.addHeader("content-type", "application/json");
        request.setEntity(params);
        HttpResponse response = httpClient.execute(request);
        HttpEntity entity = response.getEntity();
        responseString = EntityUtils.toString(entity, "UTF-8");


    }catch (Exception ex) {
        ex.printStackTrace();
        // handle exception here
    } finally {
        httpClient.getConnectionManager().shutdown();
    }
    return responseString;
}

我怀疑,这样我做的HTTP POST请求是在新设备有点错误(这是我能想到的唯一原因,因为它的工作原理我的设备上完美,但较新的设备上没有)

我也试图创建一个使用此方法(我有时看到解决问题)我的HttpClient对象,但它并没有在所有帮助:

static private HttpClient createHttpClient()
{
    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);
}

一直在它8小时左右已经扫描所有的互联网,但没有解决方案。

感谢您的帮手。

Answer 1:

那么,大量的挫折后,无论谁被套牢了同样的问题的答案:不使用Apache的HttpClient的。

更改为HttpURLConnection类似乎perfetly解决错误。



文章来源: Android HTTPs post fails on newer devices