我通过在 app.yaml 文件中添加“secure:always”行来升级我的应用程序引擎服务器,使其仅使用安全 HTTP 请求 (HTTPS)。
一切正常,我确实设法使用我的应用程序(在 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);
}
接下来我可以尝试什么?
好吧,在经历了很多挫折之后,对于遇到同样问题的人来说,答案是:不要使用 apache 的 HttpClient。
更改为 HttpURLConnection 似乎可以完美解决该错误。