如何获取字符串形式的 HTTP 响应正文?
我知道曾经有一种方法可以通过 Apache Commons 获取它,如下所示:
http://hc.apache.org/httpclient-legacy/apidocs/org/apache/commons/httpclient/HttpMethod.html
...这里有一个示例:
http://www.kodejava.org/examples/416.html
...但我相信这是已弃用。
有没有其他方法可以在 Java 中发出 http get 请求并以字符串而不是流的形式获取响应正文?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
这是我的工作项目中的两个示例。
使用
EntityUtils
和HttpEntity
< /a>使用
BasicResponseHandler
Here are two examples from my working project.
Using
EntityUtils
andHttpEntity
Using
BasicResponseHandler
我能想到的每个库都会返回一个流。您可以使用
IOUtils.toString()
来自 Apache Commons IO 在一个方法调用中将InputStream
读取到String
中。例如:更新:我更改了上面的示例,以使用响应中的内容编码(如果可用)。否则,它会默认为 UTF-8 作为最佳猜测,而不是使用本地系统默认值。
Every library I can think of returns a stream. You could use
IOUtils.toString()
from Apache Commons IO to read anInputStream
into aString
in one method call. E.g.:Update: I changed the example above to use the content encoding from the response if available. Otherwise it'll default to UTF-8 as a best guess, instead of using the local system default.
这是我正在使用 Apache 的 httpclient 库处理的另一个简单项目的示例:
只需使用 EntityUtils 来获取字符串形式的响应正文。很简单。
Here's an example from another simple project I was working on using the httpclient library from Apache:
just use EntityUtils to grab the response body as a String. very simple.
这在特定情况下相对简单,但在一般情况下相当棘手。
答案取决于
Content-Type
HTTP 响应标头。该标头包含有关有效负载的信息,并且可能定义文本数据的编码。即使您假设文本类型,您也可能需要检查内容本身才能确定正确的字符编码。 例如,请参阅 HTML 4 规范 了解详细信息关于如何针对特定格式执行此操作。
一旦编码已知,InputStreamReader 可用于解码数据。
这个答案取决于服务器是否做正确的事情 - 如果您想处理响应标头与文档不匹配的情况,或者文档声明与所使用的编码不匹配的情况,那就是另一回事了。< /em>
This is relatively simple in the specific case, but quite tricky in the general case.
The answer depends on the
Content-Type
HTTP response header.This header contains information about the payload and might define the encoding of textual data. Even if you assume text types, you may need to inspect the content itself in order to determine the correct character encoding. E.g. see the HTML 4 spec for details on how to do that for that particular format.
Once the encoding is known, an InputStreamReader can be used to decode the data.
This answer depends on the server doing the right thing - if you want to handle cases where the response headers don't match the document, or the document declarations don't match the encoding used, that's another kettle of fish.
下面是使用 Apache HTTP 客户端库以字符串形式访问响应的简单方法。
Below is a simple way of accessing the response as a String using Apache HTTP Client library.
麦克道尔的答案是正确的。但是,如果您尝试上面几篇文章中的其他建议。
然后它会给你非法状态异常,指出内容已经被消耗。
The Answer by McDowell is correct one. However if you try other suggestion in few of the posts above.
Then it will give you illegalStateException stating that content is already consumed.
就这个怎么样?
How about just this?
这是一个普通的 Java 答案:
Here is a vanilla Java answer:
我们还可以使用下面的代码来获取java中的HTML响应
We can use the below code also to get the HTML Response in java
这是一种轻量级的方法:
当然
responseString
包含网站的响应,并且响应是HttpResponse
类型,由HttpClient.execute(request)
Here's a lightweight way to do so:
With of course
responseString
containing website's response and response being type ofHttpResponse
, returned byHttpClient.execute(request)
以下代码片段显示了将响应正文作为字符串处理的更好方法,无论它是 HTTP POST 请求的有效响应还是错误响应:
Following is the code snippet which shows better way to handle the response body as a String whether it's a valid response or error response for the HTTP POST request:
如果您使用 Jackson 反序列化响应正文,一个非常简单的解决方案是使用
request.getResponseBodyAsStream()
而不是request.getResponseBodyAsString()
If you are using Jackson to deserialize the response body, one very simple solution is to use
request.getResponseBodyAsStream()
instead ofrequest.getResponseBodyAsString()
使用 Apache commons Fluent API,可以按如下方式完成:
Using Apache commons Fluent API, it can be done as mentioned below,