Example of the POST method in HttpClient in jsp, httpclientpost

Source: Internet
Author: User

Example of the POST method in HttpClient in jsp, httpclientpost

Detailed description of the POST Method Instance in HttpClient in jsp

The POST method is used to send a Request to the target server, requiring it to accept the entity attached to the Request and treat it as a Request queue (Request-Line) add a new sub-item to the resource specified by the request URI in. POST is designed to implement the following functions in a uniform way:

  1. Comment on existing resources
  2. Send messages to bulletin boards, newsgroups, email lists, or similar discussion groups
  3. Submit data blocks. For example, submit the form results to the data processing process.
  4. Expand the database through additional operations

Calling PostMethod in HttpClient is similar to GetMethod. Except for the difference between the PostMethod instance and GetMethod, the remaining steps are similar.

The steps before constructing PostMethod are the same. Like GetMethod, constructing PostMethod requires a URI parameter. In this example, the logon address is http://www.newsmth.net/bbslogin2.php. After the PostMethod instance is created, you need to fill in the Form value for the method instance. In the BBS login form, there must be two fields. The first one is the user name (domain name is id ), the second is the password (the domain name is passwd ). The fields in the form are represented by NameValuePair. The first parameter of the constructor of this class is the domain name, and the second parameter is the value of this field. The setRequestBody method is used to set all the values in the form to PostMethod. In addition, after successfully logging on to the BBS, it will switch to another page. However, HttpClient does not support automatic forwarding for requests that require subsequent services, such as POST and PUT, therefore, you must handle the page redirection. For details about page redirection, see the "Automatic redirection" section below. The Code is as follows:

String url = "http://www.newsmth.net/bbslogin2.php"; PostMethod postMethod = new PostMethod (url); // enter the value of each form field NameValuePair [] data = {new NameValuePair ("id ", "youUserName"), new NameValuePair ("passwd", "yourPwd")}; // put the form value in postMethod. setRequestBody (data); // execute postMethod int statusCode = httpClient.exe cuteMethod (postMethod); // HttpClient for requests that require receiving the next service, such as POST and PUT cannot automatically process forwarding // 301 or 302 If (statusCode = HttpStatus. SC _MOVED_PERMANENTLY | statusCode = HttpStatus. SC _MOVED_TEMPORARILY) {// retrieve the redirection Address Header locationHeader = postMethod. getResponseHeader ("location"); String location = null; if (locationHeader! = Null) {location = locationHeader. getValue (); System. out. println ("The page was redirected to:" + location);} else {System. err. println ("Location field value is null. ");} return ;}

Thank you for reading this article. I hope it will help you. Thank you for your support for this site!

Related Article

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.