gpt4 book ai didi

Java REST 分块编码

转载 作者:行者123 更新时间:2023-12-02 00:36:14 26 4
gpt4 key购买 nike

我正在尝试使用使用 block 编码的 REST 服务读取一些信息。

String encodedURL = URLEncoder.encode(url, "UTF-8");
WebClient client = org.apache.cxf.jaxrs.client.WebClient.create(encodedURL).accept("text/html");
Response response = client.get();

响应包含状态、元数据和实体。元数据包含以下信息:

{Date=[Thu, 13 Oct 2011 13:27:02 GMT], Vary=[Accept-Encoding, User-Agent], Transfer-Encoding=[chunked], Keep-Alive=[timeout=15, max=100], Content-Type=[text/html; charset=charset=UTF-8], Connection=[Keep-Alive], X-Pad=[avoid browser bug], Server=[Apache/2.2.3 (Linux/SUSE)]}

并且该实体包含 sun.net.www.protocol.http.HttpURLConnection$HttpInputStream 类型的实例。

过去,我一直使用以下代码行来获取整个结果字符串:

String resultString = client.get(String.class);

但不知何故,这一行抛出了异常:

.Problem with reading the response message, class : class java.lang.String, ContentType : text/html;charset=charset=UTF-8. org.apache.cxf.jaxrs.client.ClientWebApplicationException: .Problem with reading the response message, class : class java.lang.String, ContentType : text/html;charset=charset=UTF-8.

... caused by:

Caused by: java.io.UnsupportedEncodingException: charset=UTF-8 at sun.nio.cs.StreamDecoder.forInputStreamReader(Unknown Source) at java.io.InputStreamReader.(Unknown Source) at org.apache.cxf.helpers.IOUtils.toString(IOUtils.java:180) at org.apache.cxf.helpers.IOUtils.toString(IOUtils.java:166) at org.apache.cxf.jaxrs.provider.PrimitiveTextProvider.readFrom(PrimitiveTextProvider.java:51) at org.apache.cxf.jaxrs.client.AbstractClient.readBody(AbstractClient.java:435) ... 49 more

是否有一个简单的解决方案来获取响应的全部内容?

谢谢!

最佳答案

您可以使用

@Produces("application/json; charset=UTF-8")

jax-rs 服务的注释

关于Java REST 分块编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7754696/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com