究竟返回EntityUtils.toString(响应)? [英] What exactly returns EntityUtils.toString(response)?

查看:6178
本文介绍了究竟返回EntityUtils.toString(响应)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做我的REST API,我看到,在很多例子人使用 EntityUtils.toString(响应)来得到他们的响应字符串 GET POST PUT 删除方法。与此类似:

I'm doing my API REST and I saw that in a lot of examples people use EntityUtils.toString(response) to get their response into a String from their GET, POST, PUT and DELETE methods. Similar to this:

HttpGet method = new HttpGet(url);
method.setHeader("content-type", "application/json");
HttpResponse response = httpClient.execute(method);
String responseString = EntityUtils.toString(response.getEntity());

请避免地对我说,它给我这个字符串解答

我知道,它返回给我字符串与实体的内容(在这种情况下的反应),但这里是我的疑惑来,因为我不安全的什么这个字符串的回报。只见它进入官方文档。

I know that it returns to me a String with the content of an entity (in this case the response) but here is where my doubts come, because I'm not secure about what this String returns. I saw it into the official documentation.

阅读一个实体的内容,并返回一个字符串。

Read the contents of an entity and return it as a String.

<一个href=\"https://hc.apache.org/httpcomponents-core-ga/httpcore/apidocs/org/apache/http/util/EntityUtils.html#toString(org.apache.http.HttpEntity,java.lang.String)\" rel=\"nofollow\">https://hc.apache.org/httpcomponents-core-ga/httpcore/apidocs/org/apache/http/util/EntityUtils.html#toString(org.apache.http.HttpEntity,java.lang.String)

这是内容类型什么是在返回字符串

相反,是我和我的方法是什么在返回获取值字符串

On the contrary, are the values that I'm getting with my method what are returned in the String?

我觉得这是第二个问题,但我不保证这一点。并且,在情况下,它是真实的,这个值是如何存储到字符串?他们是否用逗号或一些特殊的字符分隔?

I think it is the second one question but I'm not secure about that. And, in the case it is true, how this values are stored into the String? Are they separated by comas or some special character?

在此先感谢!

推荐答案

这是 HttpEntity 重新presents一个HTTP响应的主体内容。 EntityUtils.toString(HttpEntity)间$ P $点内容为字符串并返回给你。

An HttpEntity represents the content of the body of an HTTP response. EntityUtils.toString(HttpEntity) interprets that content as a String and returns it to you.

如果您的HTTP响应是这样的

If your HTTP response is something like this

HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: 80

<?xml version="1.0" encoding="utf-8"?>
<root>
    <nested attr="whatever" />
</root>

然后字符串 EntityUtils.toString返回(HttpEntity)将只包含

<?xml version="1.0" encoding="utf-8"?>
<root>
    <nested attr="whatever" />
</root>

这篇关于究竟返回EntityUtils.toString(响应)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆