Android HTTPs帖子在较新的设备上失败

时间:2014-06-04 22:07:10

标签: java android google-app-engine http

几天前,我将应用引擎服务器升级为仅使用安全HTTP请求(HTTPS)。 (在app.yaml文件中添加'secure:always'行)。

一切正常,我设法通过我的应用程序(在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发布请求在新设备上有点不对(这是我能想到的唯一原因,因为它完全可以在我的设备上运行,但在新设备上却没有)

我也尝试使用这种方法创建我的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个小时已经扫描了所有互联网但没有解决方案。

感谢帮助者。

1 个答案:

答案 0 :(得分:0)

好吧,经过很多挫折之后,对于遇到同样问题的人来说,答案是:不要使用apache的HttpClient。

更改为HttpURLConnection似乎可以解决错误。