Java uses httpclient to simulate POST request and get request samples _java

Source: Internet
Author: User

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";
}
}

}

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.