Java REST Chunked Encoding
I'm trying to read some information with a REST-Service that uses chunk-encoding.
String encodedURL = URLEncoder.encode(url, "UTF-8");
WebClient client = org.apache.cxf.jaxrs.client.WebClient.create(encodedURL).accept("text/html");
Response response = client.get();
The response contains a status, metadata and the entity. The metadata contains the following information:
{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)]}
and the entity contains an instance of the type sun.net.www.protocol.http.HttpURLConnection$HttpInputStream.
I the past, I've been using the following line of of code, to get the entire result string:
String resultString = client.get(String.class);
But somehow, this line throws an exception:
.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.c开发者_如何学JAVAs.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
Is there a straightforward solution to the get the entire content of the response?
Thank you!
kon
You can use
@Produces("application/json; charset=UTF-8")
annotations for the jax-rs services
I'm afraid that the server side sends garbage and causes the exception to be thrown on the client side.
The problematic piece is the Content-type HTTP header information. It is set to:
text/html; charset=charset=UTF-8
As you can see, the word charset is repeated. Thus, your client tries to decode it using an encoding called charset=UTF-8
, which of course doesn't exist.
The best solution would be if the problem was fixed on the server side. But I don't know if you can get it fixed there. If not, the next best approach is to tried to fix the HTTP header before you try to get the response contents.
精彩评论