Pitfall: entityutils of httpcomponents

Source: Internet
Author: User

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:

 
 
  1. 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:

 
 
  1. CloseableHttpResponse httpResponse = httpClient.execute(get);
  2. HttpEntity httpResponseEntity = httpResponse.getEntity();
  3. 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?

 
 
  1. EntityUtils.toString(httpResponseEntity, "utf-8");

By the way, a pitfall that I and my colleagues have stepped on before.

 
 
  1. 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 ......

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.