"Note--java" about httpurlconnection getting data Chinese garbled problem

Source: Internet
Author: User

A time ago wrote a from Shun Fung to obtain the information of the program, through the httpurlconnection to get data garbled, clearly intercepted data write utf-8 or garbled, and finally found that the reason is no data decompression, blocking:

Finally, the problem is found, the data transfer appears to be compressed first, it is estimated that the transfer efficiency bar.

Then unzip it first:

1 /**2 * This is a static method, the Data tool method, incoming input stream in, and the data length, you can get the extracted result byte array3      * @paramin an input stream, which can be obtained by getInputStream () of the HttpURLConnection object4      * @paramlength data is mainly used to create a just byte array to hold the resulting data, can be HttpURLConnection object Getheaderfield ("Content-length") to obtain5      */6     7      Public Static byte[] GetInt (InputStream in,intlength) {8         byte[] by =New byte[length];9         Ten         Try { OneGzipinputstream Giz =NewGzipinputstream (in); AGiz.read (by, 0, length); -}Catch(IOException e) { -             //TODO auto-generated Catch block the e.printstacktrace (); -         } -         returnby ; -}

After extracting the byte[] array data, directly through the

string result = new String (by, "Utf-8");

We can get results.

which

By: Get the extracted array

Utf-8: This is the type of encoding you can see from the Intercept.

"Note--java" about httpurlconnection getting data Chinese garbled problem

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.