HttpClient submits the data source code using the Post method:
Copy Code code as follows:
Package post;
Import Java.io.IOException;
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.methods.PostMethod;
public class Posthttp {
/**
* @param args
*/
public static void Main (string[] args) {
TODO auto-generated Method Stub
HttpClient httpclient = new HttpClient ();
String url = "http://193.167.13.21/setup.cgi";
Postmethod Postmethod = new Postmethod (URL);
Fill in the values of each form field
namevaluepair[] data = {
New Namevaluepair ("ID", "11"),
New Namevaluepair ("Mtg", "0"),
New Namevaluepair ("Havecookie", "0"),
New Namevaluepair ("Backid", "30"),
New Namevaluepair ("PSW", "password")
};
Put the value of a form into Postmethod
Postmethod.setrequestbody (data);
Execute Postmethod
int statusCode = 0;
try {
StatusCode = Httpclient.executemethod (Postmethod);
catch (HttpException e) {
TODO auto-generated Catch block
E.printstacktrace ();
catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
HttpClient for requests to receive subsequent services, such as post and put are not automatically processed for forwarding
301 or 302.
if (StatusCode = = httpstatus.sc_moved_permanently | | statusCode = = httpstatus.sc_moved_temporarily)
{
Remove the address of the shift from the beginning
Header Locationheader = Postmethod.getresponseheader ("location");
String location = null;
if (Locationheader!= null) {
Location = Locationheader.getvalue ();
SYSTEM.OUT.PRINTLN ("The page is redirected to:" + location);
}
else {
System.err.println ("Location field value is null.");
}
Return
}
Else
{
System.out.println (Postmethod.getstatusline ());
String str = "";
try {
str = postmethod.getresponsebodyasstring ();
catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
System.out.println (str);
}
Postmethod.releaseconnection ();
return;
}
}
This is a user authentication process of landing information
The jar package that you need:
1, Commons-httpclient-3.1-rc1.zip http://jakarta.apache.org/commons/httpclient/downloads.html
2, Commons-codec-1.3.jar http://jakarta.apache.org/site/downloads/downloads_commons-codec.cgi
3, commons-logging-api.jar tomcat5.5 in the Tomcat 5.5/bin directory or http:// jakarta.apache.org/site/downloads/downloads_commons-logging.cgi