Detailed Explanation of POST Method Examples in jsp and HttpClient

  • 2021-12-13 08:59:17
  • OfStack

Detailed explanation of POST method example in HttpClient in jsp

The POST method is used to issue a request to the destination server to accept the entity attached to the request as an additional new subentry in the request queue (Request-Line) requesting the resource specified by URI. POST is designed to perform the following functions using the Uniform 1 approach:

Comments on existing resources
Send messages to electronic bulletin boards, newsgroups, mailing lists, or similar discussion groups
Submit data blocks, such as the results of a form, to the data processing process
Extend the database through additional operations

Calling PostMethod in HttpClient is similar to GetMethod, except that setting up an instance of PostMethod is somewhat different from GetMethod, the remaining steps are similar.

The steps before constructing PostMethod are the same. Like GetMethod1, constructing PostMethod also requires an URI parameter. In this example, the login address is http://www.newsmth.NET/bbslogin2.PHP. After creating the instance of PostMethod, you need to fill the method instance with the values of the form. In the login form of BBS, you need to have two fields, the first is the user name (domain name id) and the second is the password (domain name passwd). The domain in the form is represented by the class NameValuePair, the first parameter of the constructor of this class is the domain name, and the second parameter is the value of the domain; Set all the values of the form to PostMethod using method setRequestBody. In addition, BBS will turn to another page after successful login, but HttpClient does not support automatic forwarding for requests requiring subsequent services, such as POST and PUT, so it needs to handle the page turning by itself. See the "Automatic Turning" section below for specific page turning processing. The code is as follows:


String url = "http://www.newsmth.net/bbslogin2.php"; 
PostMethod postMethod = new PostMethod(url); 
//  Fill in the values of individual form fields  
NameValuePair[] data = { new NameValuePair("id", "youUserName"), 
new NameValuePair("passwd", "yourPwd") }; 
//  Put the value of the form into the postMethod Medium  
postMethod.setRequestBody(data); 
//  Execute postMethod 
int statusCode = httpClient.executeMethod(postMethod); 
// HttpClient For a request to accept a successor service, such as POST And PUT Can't automatically process forwarding, etc.  
// 301 Or 302 
if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY ||  
statusCode == HttpStatus.SC_MOVED_TEMPORARILY) { 
  //  Take out the turning address from the head  
  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, hope to help everyone, thank you for your support to this site!


Related articles: