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:
- Comment on existing resources
- Send messages to bulletin boards, newsgroups, email lists, or similar discussion groups
- Submit data blocks. For example, submit the form results to the data processing process.
- 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!