HttpClient GET request and POST request example

Source: Internet
Author: User
Tags response code

HttpClient GET request and POST request example Blog Category: 
    • Java synthesis

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
  1. Package com.wujintao.httpclient;
  2. Import java.io.IOException;
  3. Import Java.io.InputStream;
  4. Import Org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
  5. Import org.apache.commons.httpclient.HttpClient;
  6. Import org.apache.commons.httpclient.HttpException;
  7. Import Org.apache.commons.httpclient.HttpStatus;
  8. Import Org.apache.commons.httpclient.NameValuePair;
  9. Import Org.apache.commons.httpclient.methods.GetMethod;
  10. Import Org.apache.commons.httpclient.methods.PostMethod;
  11. Import Org.apache.commons.httpclient.params.HttpMethodParams;
  12. Import Org.junit.Test;
  13. Public class TestCase {
  14. @Test
  15. public void Testgetrequest () throws IllegalStateException, IOException {
  16. HttpClient client = new HttpClient ();
  17. StringBuilder sb = new StringBuilder ();
  18. InputStream ins = null;
  19. //Create a method instance.
  20. GetMethod method = New GetMethod ("http://www.baidu.com");
  21. //Provide custom retry handler is necessary
  22. Method.getparams (). Setparameter (Httpmethodparams.retry_handler,
  23. New Defaulthttpmethodretryhandler (3, false));
  24. try {
  25. //Execute the method.
  26. int statusCode = Client.executemethod (method);
  27. System.out.println (StatusCode);
  28. if (StatusCode = = HTTPSTATUS.SC_OK) {
  29. INS = Method.getresponsebodyasstream ();
  30. byte[] B = new byte[1024];
  31. int r_len = 0;
  32. While ((R_len = Ins.read (b)) > 0) {
  33. Sb.append (new String (b, 0, R_len, method
  34. . Getresponsecharset ()));
  35. }
  36. } Else {
  37. System.err.println ("Response Code:" + statusCode);
  38. }
  39. } catch (HttpException e) {
  40. System.err.println ("Fatal protocol violation:" + e.getmessage ());
  41. } catch (IOException e) {
  42. System.err.println ("Fatal Transport Error:" + e.getmessage ());
  43. } finally {
  44. Method.releaseconnection ();
  45. if (ins! = null) {
  46. Ins.close ();
  47. }
  48. }
  49. System.out.println (Sb.tostring ());
  50. }
  51. @Test
  52. public void Testpostrequest () throws HttpException, IOException {
  53. HttpClient client = new HttpClient ();
  54. Postmethod method = New Postmethod ("Http://www.baidu.com/getValue");
  55. Method.setrequestheader ("Content-type",
  56. "application/x-www-form-urlencoded;charset=gb2312");
  57. namevaluepair[] param = { new Namevaluepair ("age", "one"),
  58. New Namevaluepair ("name", "Jay"),};
  59. Method.setrequestbody (param);
  60. int statusCode = Client.executemethod (method);
  61. System.out.println (StatusCode);
  62. Method.releaseconnection ();
  63. }
  64. }

HttpClient GET request and POST request example

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.