Java using httpclient analog post form operation application and Considerations _java

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.