HttpClient GET request and POST request example
Blog Category:
HttpClient supports all HTTP methods defined in the http/1.1 specification: GET, HEAD, POST, PUT, DELETE, TRACE, and OPTIONS. Each method has a corresponding class: Httpget,httphead,httppost,httpput,httpdelete,httptrace and Httpoptions. All of these classes implement the Httpurirequest interface, so they can be used as execution parameters for execute. The request URI is the Uniform Resource identifier that can apply the request. The URI of the HTTP request contains a protocol plan protocol scheme, hostname host name, optional port optional ports, path to resource resource path, optional query optional Query and the optional fragment optional fragment.
Head,put,delete,trace HttpClient supports these methods,
Most browsers do not support these methods, because the method methods for FORM in HTML 4 support only two get and post, and many browsers are still based on HTML4.
Usually in Java through the code call URL for the remote method call, these methods are get request way, there is a POST request mode, for this, summarize an example, posted for review.
Dependent jar packages such as:
Example code:
Java code
- Package com.wujintao.httpclient;
- Import java.io.IOException;
- Import Java.io.InputStream;
- Import Org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
- 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.GetMethod;
- Import Org.apache.commons.httpclient.methods.PostMethod;
- Import Org.apache.commons.httpclient.params.HttpMethodParams;
- Import Org.junit.Test;
- Public class TestCase {
- @Test
- public void Testgetrequest () throws IllegalStateException, IOException {
- HttpClient client = new HttpClient ();
- StringBuilder sb = new StringBuilder ();
- InputStream ins = null;
- //Create a method instance.
- GetMethod method = New GetMethod ("http://www.baidu.com");
- //Provide custom retry handler is necessary
- Method.getparams (). Setparameter (Httpmethodparams.retry_handler,
- New Defaulthttpmethodretryhandler (3, false));
- try {
- //Execute the method.
- int statusCode = Client.executemethod (method);
- System.out.println (StatusCode);
- if (StatusCode = = HTTPSTATUS.SC_OK) {
- INS = Method.getresponsebodyasstream ();
- byte[] B = new byte[1024];
- int r_len = 0;
- While ((R_len = Ins.read (b)) > 0) {
- Sb.append (new String (b, 0, R_len, method
- . Getresponsecharset ()));
- }
- } Else {
- System.err.println ("Response Code:" + statusCode);
- }
- } catch (HttpException e) {
- System.err.println ("Fatal protocol violation:" + e.getmessage ());
- } catch (IOException e) {
- System.err.println ("Fatal Transport Error:" + e.getmessage ());
- } finally {
- Method.releaseconnection ();
- if (ins! = null) {
- Ins.close ();
- }
- }
- System.out.println (Sb.tostring ());
- }
- @Test
- public void Testpostrequest () throws HttpException, IOException {
- HttpClient client = new HttpClient ();
- Postmethod method = New Postmethod ("Http://www.baidu.com/getValue");
- Method.setrequestheader ("Content-type",
- "application/x-www-form-urlencoded;charset=gb2312");
- namevaluepair[] param = { new Namevaluepair ("age", "one"),
- New Namevaluepair ("name", "Jay"),};
- Method.setrequestbody (param);
- int statusCode = Client.executemethod (method);
- System.out.println (StatusCode);
- Method.releaseconnection ();
- }
- }
HttpClient GET request and POST request example