本文介绍了我如何使用命令:HttpEntity?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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



  //实例化自定义HttpClient 
DefaultHttpClient client = new MyHttpClient(getApplicationContext());
HttpGet get = new HttpGet(https://www.mydomain.ch/rest/contacts/23);
//执行GET调用并获取响应
HttpResponse getResponse = client.execute(get);
HttpEntity responseEntity = getResponse.getEntity();

如上所述,我在最后一行之后遇到问题。我该如何处理responseEntity?如果我希望在WebView上显示https网站,我该如何使用它?一些帮助和解释会很好:)

解决方案

如果你想要 HttpEntity中的内容 正确方式包括调用 HttpEntity#getContent()来检索流并执行Android SDK中已经提供了大量毫无意义的东西。



试试这个。

  //执行GET调用并获取响应
HttpResponse getResponse = client.execute(get);
HttpEntity responseEntity = getResponse.getEntity();

//从响应实体中检索字符串
String content = EntityUtils.toString(responseEntity);

//现在内容将包含服务器响应的任何内容,你
//可以使用#loadDataWithBaseURL


考虑使用 > content - 它表现得更好。


I wish to settle my long term problem by this question and hope you guys would help, but firstly; I have been having issues to connect to a HTTPS self-signed certificate server for almost 3 weeks. Despite the multiple solutions here, I cannot seem to resolve my problem. Probably I did not know how to use it properly or did not have some files or imported the correct libraries.

I came across some websites that requires me to download a certificate from the https site that I am trying to connect into, and when I did that. I have to do the some steps before I can use the certificate or keystore that I created. I got this solution from this website:

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();

I have a problem, after the last line, as stated above. What do I do with the responseEntity? How do I use it if I wish to display the https website on a WebView? Some help and explanation would be nice :)

解决方案

If you want the content from the HttpEntity the correct way does not include calling HttpEntity#getContent() to retrieve a stream and doing tons of pointless stuff already available in the Android SDK.

Try this instead.

// 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

Consider using WebView#loadDataWithBaseURL when displaying content - it behaves a lot nicer.

这篇关于我如何使用命令:HttpEntity?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-09 20:35