N S*_*rma 4 ssl https android web-services
我正在更新android应用程序的Web服务URL,我们正在使用https协议.我看到我当前的https网址正在运行,但现在我们正在迁移到新域,然后它正在创建问题.
我在stackoverflow上检查了许多线程,如javax.net.ssl.SSLException:证书中的主机名与android不匹配,但没有找到任何好的答案,主要是回答绕过此安全或允许所有.
javax.net.ssl.SSLException:证书中的主机名不匹配:
//HttpGet getMethod = new HttpGet(String.format(httpURL));
HttpGet getMethod = new HttpGet(httpURL);
DefaultHttpClient client = new DefaultHttpClient();
ResponseHandler<String> responseHandler = new BasicResponseHandler();
HttpParams params = client.getParams();
params.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 60000);
params.setParameter(CoreConnectionPNames.SO_TIMEOUT, 60000);
client.setParams(params);
responseBody = client.execute(getMethod, responseHandler);
responseBody = responseBody.trim();
Run Code Online (Sandbox Code Playgroud)
提前致谢.
似乎最好的解决方案是使用HttpsUrlConnection而不是HttpGet.
URL url = new Url(httpURL);
HttpURLConnection urlConnection = (HttpsURLConnection) url.openConnection();
urlConnection.setReadTimeout(60000);
urlConnection.setConnectTimeout(60000);
urlConnection.setRequestMethod("GET");
urlConnection.setDoInput(true);
urlConnection.connect();
Run Code Online (Sandbox Code Playgroud)
然后使用InputStream获取响应主体.
InputStream inputStream = urlConnection.getInputStream();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8427 次 |
| 最近记录: |