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;
}
}