I am calling a restful service that returns JSON using the Apache HttpClient.
The problem is I am getting different results in the encoding of the response when I run the code on different platforms.
Here is my code:
GetMethod get = new GetMethod("http://urltomyrestservice");
get.addRequestHeader("Content-Type", "text/html; charset=UTF-8");
...
HttpResponse response = httpexecutor.execute(request, conn, context);
response.setParams(params);
httpexecutor.postProcess(response, httpproc, context);
StringWriter writer = new StringWriter();
IOUtils.copy(response.getEntity().getContent(), writer);
When I run this on OSX, asian characters etc return fine e.g. 張惠妹 in the response. But when I run this on a linux server the same code displays the characters as ???
The linux server is an Amazon EC2 instance running Java 1.6.0_26-b03
My local OSX is running 1.6.0_29-b11
Any ideas really appreciated!!!!!
If you look at the javadoc of
org.apache.commons.io.IOUtils.copy(InputStream, Writer):So that will give different answers depending on the client (which is what you’re seeing)
Also,
Content-Typeis usually a response header (unless you’re using POST or PUT). The server is likely to ignore it (though you might have more luck with theAccept-Charsetrequest header).You need to parse the content type’s charset-encoding parameter of the response header, and use that to convert the response into a
String(if it’s aStringyou’re actually after). I expect Commons HTTP has code that will do that automatically for you. If it doesn’t, Spring’sRESTTemplatedefinitely does.