Some websites first determine whether the user's request is from the browser. If not, the system returns incorrect text. Therefore, when capturing information with httpclient, the following information is added to the header:
Header. put ("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; sv1; qqdownload 1.7 ;. net CLR 1.1.4322; CBA ;. net CLR 2.0.50727 )");
By default
The User-Agent value is Jakarta commons-httpclient 3.0rc1. To change it (for example, to Mozilla/4.0), you must run the following statement before calling it:
System. getproperties (). setproperty ("httpclient. useragent", "Mozilla/4.0 ");
// Set the HTTP Header
List Headers. Add (new header ("User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1 )"));
Httpclient. gethostconfiguration (). getparams (). setparameter ("HTTP. Default-headers", headers );
Getmethod method = new getmethod ("http://www.baidu.com ");
Method. getparams (). setparameter (httpmethodparams. retry_handler,
New defaulthttpmethodretryhandler (3, false ));
Try {
Int statuscode = httpclient.exe cutemethod (method );
If (statuscode! = Httpstatus. SC _ OK ){
System. Out. println ("method failed code =" + statuscode + ":" + method. getstatusline ());
} Else {
System. Out. println (new string (method. getresponsebody (), "gb2312 "));
}
} Finally {
Method. releaseconnection ();
}
}
}