我希望通过这个问题来解决我的长期问题,并希望你们能帮助我,但首先,我在连接HTTPS自签名证书服务器时遇到了近3周的问题。尽管这里有多种解决方案,但我似乎无法解决我的问题。可能我不知道如何正确使用它,或者没有一些文件或导入了正确的库。
我遇到了一些网站,它们要求我从我尝试连接的https站点下载证书,以及我何时这样做。在使用我创建的证书或密钥库之前,我必须执行一些步骤。我从这个网站得到了这个解决方案:
Android: Trusting SSL certificates
// Instantiate the custom HttpClient
DefaultHttpClient client = new MyHttpClient(getApplicationContext());
HttpGet get = new HttpGet("https://www.mydomain.ch/rest/contacts/23");
// Execute the GET call and obtain the response
HttpResponse getResponse = client.execute(get);
HttpEntity responseEntity = getResponse.getEntity();我有一个问题,在最后一行之后,如上所述。我该如何处理responseEntity?如果我希望在WebView上显示https网站,该如何使用?一些帮助和解释将是很好的:)
发布于 2012-03-13 15:22:37
您需要调用responseEntity.getContent()以在InputStream中获取针对您请求的URL的响应。以您想要的方式使用该流来表示数据。例如,如果期望的数据是String,那么您可以使用以下方法简单地将该流转换为string:
/**
* Converts InputStream to String and closes the stream afterwards
* @param is Stream which needs to be converted to string
* @return String value out form stream or NULL if stream is null or invalid.
* Finally the stream is closed too.
*/
public static String streamToString(InputStream is) {
try {
StringBuilder sb = new StringBuilder();
BufferedReader tmp = new BufferedReader(new InputStreamReader(is),65728);
String line = null;
while ((line = tmp.readLine()) != null) {
sb.append(line);
}
//close stream
is.close();
return sb.toString();
}
catch (IOException e) { e.printStackTrace(); }
catch (Exception e) { e.printStackTrace(); }
return null;
}https://stackoverflow.com/questions/9679673
复制相似问题