In a service program written today, someone reported that the obtained data was garbled in Chinese. I used apache to retrieve data through httpcomponents, so I started the debug level of the log.
In the log, we found that Chinese characters were missing and garbled:
2014-07-02 16:35:01.348 DEBUG [Wire.java:86] http-outgoing-8 << "<?xml version="1.0" encoding="UTF-8"?>... subject="[0xe6][0x88][0x91][0xe6][0x98][0xaf][0xe4][0xb8][0xad][0xe6][0x96][0x87][0xe4][0xb8][0xbb][0xe9][0xa2][0x98]" ...
How can I garbled packets? I set UTF-8 encoding!
In fact, this is the first pitfall: When httpcomponents logs, it converts Chinese to this format. Actually, it's right!
Sorry, I 've been in this trap for a long time before I found out!
After searching for half a day, I finally found that all the Chinese newspapers I sent and received were correct, but the Chinese text I found was garbled. After a long time, I found that no encoding was returned when the remote server returned, and the Code for getting the package body was using entityutils:
CloseableHttpResponse httpResponse = httpClient.execute(get);
HttpEntity httpResponseEntity = httpResponse.getEntity();
String s = EntityUtils.toString(httpResponseEntity);
No problem! However, this is a big pitfall,The Default Code of httpcomponents is not UTF-8.!
So this S is messy ......
Which of the following statements is true?
EntityUtils.toString(httpResponseEntity, "utf-8");
By the way, a pitfall that I and my colleagues have stepped on before.
EntityUtils.toString(httpResponseEntity, "utf-8");
This line of code must be called in an HTTP request! Or, the returned package must be read. Even if the returned result is not 200 OK!
Previously, it was not called because it did not care about the returned package body content. Then the first request can be successful, and the second request gets stuck ......
Even more difficult is to read the package body stream when 200 OK, and directly throw an exception or return an error. Then the program looks normal, but from time to time cards ......