The httpclient page is garbled.

Source: Internet
Author: User

Httpclient is garbled, and there are many methods on the Internet. Here, I just want to say that I have obtained my own page, and there is a garbled situation:

Add a character set to the data stream to solve this problem:

For example: "bufferedreader BR = new bufferedreader (New inputstreamreader (in," UTF-8 "));"

The source code is as follows:

Private string getpage (string URL, string encode ){
// Create an httpclient instance
Httpclient = new httpclient ();
// Set the encoding Parameters
If (encode! = NULL ){
Httpclient. getparams (). setparameter (httpmethodparams. http_content_charset, encode );

} Else {
Httpclient. getparams (). setparameter (httpmethodparams. http_content_charset, "GBK ");
}
// Ignore cookies
Httpclient. getparams (). setcookiepolicy (cookiepolicy. ignore_cookies );

// Create a getmethod instance to access the specified URL
Getmethod = new getmethod (URL );
Try {
// Access the specified URL and obtain the return status code
Int statuscode = httpclient.exe cutemethod (getmethod );
If (statuscode = 200) {// return the success Status Code 200
// Read the HTML source code of the page
Stringbuffer sb = new stringbuffer ();
Inputstream in = getmethod. getresponsebodyasstream ();
Bufferedreader BR = new bufferedreader (New inputstreamreader (in, "UTF-8 "));
String line;
While (line = Br. Readline ())! = NULL ){
SB. append (line );
}
If (BR! = NULL) Br. Close ();
Return sb. tostring ();
} Else {
Return NULL;
}
} Catch (exception ex ){
Ex. printstacktrace ();
Return NULL;
}
}

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.