Copy Code code as follows:
Import Java.io.BufferedReader;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.InputStreamReader;
Import Org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
Import Org.apache.commons.httpclient.Header;
Import org.apache.commons.httpclient.HttpClient;
Import org.apache.commons.httpclient.HttpException;
Import Org.apache.commons.httpclient.HttpStatus;
Import Org.apache.commons.httpclient.NameValuePair;
Import Org.apache.commons.httpclient.cookie.CookiePolicy;
Import Org.apache.commons.httpclient.methods.GetMethod;
Import Org.apache.commons.httpclient.methods.PostMethod;
Import Org.apache.commons.httpclient.params.HttpMethodParams;
public class Testhttpclient {
public static void Main (string[] args) {
TODO auto-generated Method Stub
Defining an instance of a httpclient
HttpClient httpclient = new HttpClient ();
To create an instance of a Get method
GetMethod GetMethod = new GetMethod ("http://jb51.net");
Use the system-provided default recovery policy
Getmethod.getparams (). Setparameter (Httpmethodparams.retry_handler, New Defaulthttpmethodretryhandler ());
To create a POST method instance
Postmethod Postmethod = new Utf8postmethod ("http://jb51.net");
//
Fill in the values of each form field
namevaluepair[] data = {new Namevaluepair ("user_name", "user_name"), New Namevaluepair ("Password", "password")};
//
Put the value of the form into the Post method
Postmethod.setrequestbody (data);
//
Postmethod.getparams (). Setparameter (
"Http.protocol.cookie-policy", cookiepolicy.browser_compatibility);
Postmethod.setrequestheader ("Referer", "http://jb51.net");
try{
Execute Get method
int statusCode = Httpclient.executemethod (GetMethod);
Perform post methods
int statusCode = Httpclient.executemethod (Postmethod);
if (StatusCode = = httpstatus.sc_moved_temporarily) {
Header Locationheader = Postmethod.getresponseheader ("Location");
String location = null;
if (Locationheader!= null) {
Location = Locationheader.getvalue ();
}
Postmethod = new Postmethod (location);
Postmethod.setrequestheader ("Referer", "Http://jb51.net/login");
Namevaluepair[] Data1 = {new Namevaluepair ("user_name", "user_name"), New Namevaluepair ("Password", "password")};
Postmethod.setrequestbody (DATA1);
Postmethod.getparams (). Setparameter (
"Http.protocol.cookie-policy", cookiepolicy.browser_compatibility);
int statusCode1 = Httpclient.executemethod (Postmethod);
if (statusCode1!= httpstatus.sc_ok) {
System.out.println ("method is wrong" + postmethod.getstatusline ());
}
}
if (StatusCode!= httpstatus.sc_ok) {
System.out.println ("method is wrong" + postmethod.getstatusline ());
}
InputStream responsebody = Postmethod.getresponsebodyasstream ();
BufferedReader reader = new BufferedReader (new InputStreamReader (Responsebody, "utf-8"));
String line = Reader.readline ();
while (line!= null) {
System.out.println (New String (Line.getbytes ()));
line = Reader.readline ();
}
}
catch (HttpException e) {
Todo:handle exception
System.out.println ("Please check your provided HTTP address!");
E.printstacktrace ();
}catch (IOException e) {
Todo:handle exception
SYSTEM.OUT.PRINTLN ("The line is wrong!");
E.printstacktrace ();
}finally{
Getmethod.releaseconnection ()//Free link
Postmethod.releaseconnection ();
}
}
Inner Class for UTF-8 support
public static class Utf8postmethod extends postmethod{
Public utf8postmethod (String URL) {
Super (URL);
}
@Override
Public String Getrequestcharset () {
return Super.getrequestcharset ();
return "UTF-8";
}
}
}