0

我希望通过这个问题来解决我的长期问题,并希望你们能提供帮助,但首先;近 3 周以来,我一直在连接 HTTPS 自签名证书服务器时遇到问题。尽管这里有多种解决方案,但我似乎无法解决我的问题。可能我不知道如何正确使用它或者没有一些文件或导入正确的库。

我遇到了一些网站,要求我从我尝试连接的 https 网站下载证书,以及何时下载。在使用我创建的证书或密钥库之前,我必须执行一些步骤。我从这个网站得到了这个解决方案:

Android:信任 SSL 证书

// 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 网站,如何使用它?一些帮助和解释会很好:)

4

3 回答 3

4

如果您希望以正确的方式获取内容,HttpEntity则不包括调用检索HttpEntity#getContent()流和执行 Android SDK 中已有的大量无意义的事情。

试试这个。

// Execute the GET call and obtain the response
HttpResponse getResponse = client.execute(get);
HttpEntity responseEntity = getResponse.getEntity();

// Retrieve a String from the response entity
String content = EntityUtils.toString(responseEntity);

// Now content will contain whatever the server responded with and you
// can pass it to your WebView using #loadDataWithBaseURL

考虑在显示时使用WebView#loadDataWithBaseURLcontent - 它的表现要好得多。

于 2012-03-13T09:34:29.147 回答
0
InputStream is = responseEntity.getContent();
 try{
 BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
        StringBuilder sb = new StringBuilder();
         sb.append(reader.readLine() + "\n");
         String line="0";
         while ((line = reader.readLine()) != null) {
                        sb.append(line + "\n");
          }

     String   result=sb.toString();
          is.close();
 }catch(Exception e){
                Log.e("log_tag", "Error converting result "+e.toString());
          }

您将拥有字符串“结果”中的所有内容

于 2012-03-13T07:26:30.113 回答
0

您需要在InputStreamresponseEntity.getContent()中调用以获取针对您请求的 URL 的响应。以您的方式使用该流来呈现您想要的数据。例如,如果预期的数据是字符串,那么您可以简单地将这个流转换为字符串,方法如下:

/**
 * 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;
}
于 2012-03-13T07:22:36.513 回答