Import java. Io. bufferedreader;
Import java. Io. inputstreamreader;
Import org. Apache. http. httpentity;
Import org. Apache. http. httphost;
Import org. Apache. http. httpresponse;
Import org. Apache. http. Client. Methods. httppost;
Import org. Apache. http. Conn. Params. connroutepnames;
Import org. Apache. http. entity. stringentity;
Import org. Apache. http. impl. Client. defaulthttpclient;
/**
* Httpclient 4 uses the POST method to submit common form data.
*
* @ Author Java century network (java2000.net, laozizhu.com)
*/
Public class httpclientpost {
Public static void main (string [] ARGs) throws exception {
Defaulthttpclient httpclient = new defaulthttpclient ();
// Proxy settings
Httphost proxy = new httphost ("10.60.8.20", 8080 );
Httpclient. getparams (). setparameter (connroutepnames. default_proxy, proxy );
// Target address
Httppost = new httppost ("http://www.java2000.net/login.jsp ");
System. Out. println ("Request:" + httppost. getrequestline ());
// Construct the simplest string data
Stringentity reqentity = new stringentity ("username = Test & Password = test ");
// Set the type
Reqentity. setcontenttype ("application/X-WWW-form-urlencoded ");
// Set the requested data
Httppost. setentity (reqentity );
// Execute
Httpresponse response = httpclient.exe cute (httppost );
Httpentity entity = response. getentity ();
System. Out. println ("----------------------------------------");
System. Out. println (response. getstatusline ());
If (entity! = NULL ){
System. Out. println ("response Content Length:" + entity. getcontentlength ());
}
// Display the result
Bufferedreader reader = new bufferedreader (New inputstreamreader (entity. getcontent (), "UTF-8 "));
String line = NULL;
While (line = reader. Readline ())! = NULL ){
System. Out. println (line );
}
If (entity! = NULL ){
Entity. consumecontent ();
}
}
}