Android的HTTPS POST失败,在较新设备 [英] Android HTTPs post fails on newer devices
问题描述
前几天我升级我的应用程序引擎服务器安全的HTTP请求只(HTTPS)工作。 (加入'安全:总是在app.yaml文件行)。
Few days ago I upgraded my app engine server to work with secure HTTP requests only (HTTPS). (by adding 'secure: always' line in the app.yaml file).
一切工作正常,我设法得到我的应用我的应用程序引擎服务器的响应(在Android 4.1.2运行),但今天我发现,在4.4.2设备,我得到以下错误:
Everything worked fine, I do manage to get response from my app engine server with my app (running on android 4.1.2), but today I found out that on 4.4.2 devices, I get the following error:
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)
这是在客户端的code负责发送一个JSON与HTTP POST请求:
This is the code in the client responsible for sending a json with http post request:
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请求是在较新的设备(这是我能想到的唯一原因,因为它的工作原理我的设备上完美,但较新的设备上它不会)有些错
I suspect that this way I'm doing HTTP post requests is somewhat wrong in newer devices (that's the only reason I can think of, since it works on my device perfectly, but on newer device it doesn't)
我也尝试创建使用这种方法(我有时看到解决问题)我的HttpClient对象,但它并没有在所有帮助:
I also tried to create my HttpClient object using this method (which I saw fixes the problem sometimes) but it didn't help at all:
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小时左右已经扫描所有的互联网,但没有办法了。
Been on it for around 8 hours already scanning all the internet but no solution.
感谢帮手。
推荐答案
好了,许多坎坷之后,谁被套牢了同样的问题的答案:不使用Apache的HttpClient的。
Well, after lots of frustrations, the answer for whoever is stuck with the same problem: do not use apache's HttpClient.
更改为HttpURLConnection类似乎perfetly解决错误。
Changing to HttpURLConnection seems to solve the error perfetly.
这篇关于Android的HTTPS POST失败,在较新设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!