首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用命令: HttpEntity?

如何使用命令: HttpEntity?
EN

Stack Overflow用户
提问于 2012-03-13 15:07:05
回答 2查看 7.6K关注 0票数 0

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

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

Android: Trusting SSL certificates

代码语言:javascript
复制
// 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网站,该如何使用?一些帮助和解释将是很好的:)

EN

Stack Overflow用户

发布于 2012-03-13 15:22:37

您需要调用responseEntity.getContent()以在InputStream中获取针对您请求的URL的响应。以您想要的方式使用该流来表示数据。例如,如果期望的数据是String,那么您可以使用以下方法简单地将该流转换为string:

代码语言:javascript
复制
/**
 * 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;
}
票数 0
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9679673

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档