POST java simulation of the application form HTTPCLIENT operation and matters needing attention

  Httpclient method used to post the data source code: 

  Package post; 

  Import java.io.IOException; 

  Import org.apache.commons.httpclient.Header; 
  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.PostMethod; 

  (Public class PostHttp 

  / ** 
  * @ Param args 
  * / 
  Public static void main (String [] args) ( 
  / / TODO Auto-generated method stub 
  HttpClient httpClient = new HttpClient (); 
  String url = "http://193.167.13.21/setup.cgi"; 
  PostMethod postMethod = new PostMethod (url); 
  / / Fill in the value of various forms domain 
  NameValuePair data [] = ( 
  New NameValuePair ( "ID", "11"), 
  New NameValuePair ( "mtg", "0"), 
  New NameValuePair ( "haveCookie", "0"), 
  New NameValuePair ( "backID", "30"), 
  New NameValuePair ( "psw", "password") 
  ); 
  / / Add to the value of the form in postMethod 
  PostMethod.setRequestBody (data); 
  / / Implementation postMethod 
  Int statusCode = 0; 
  Try ( 
  StatusCode = httpClient.executeMethod (postMethod); 
  ) Catch (HttpException e) ( 
  / / TODO Auto-generated catch block 
  E.printStackTrace (); 
  ) Catch (IOException e) ( 
  / / TODO Auto-generated catch block 
  E.printStackTrace (); 
  ) 
  / / HttpClient to receive follow-up services for the request, such as POST and PUT be processed automatically transmitted 
  / / 301 or 302 
  If (HttpStatus.SC_MOVED_PERMANENTLY statusCode == | | == HttpStatus.SC_MOVED_TEMPORARILY statusCode) 
  ( 
  / / Removed from scratch to 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; 
  ) 
  Else 
  ( 
  System.out.println (postMethod.getStatusLine ()); 
  String str = ""; 
  Try ( 
  PostMethod.getResponseBodyAsString str = (); 
  ) Catch (IOException e) ( 
  / / TODO Auto-generated catch block 
  E.printStackTrace (); 
  ) 
  System.out.println (str); 
  ) 
  PostMethod.releaseConnection (); 
  Return; 
  ) 

  ) 
  / / This is a user authentication information during landing 

  Which needs jar package: 

  1, commons-httpclient-3.1-rc1.zip http://jakarta.apache.org/commons/httpclient/downloads.html 

  2, commons-codec-1.3.jar http://jakarta.apache.org/site/downloads/downloads_commons-codec.cgi 

  3, commons-logging-api.jar in tomcat5.5 in Tomcat 5.5 \ bin directory or http://jakarta.apache.org/site/downloads/downloads_commons-logging.cgi 

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Facebook
  • DotNetKicks
  • DZone
  • Netvouz
  • Propeller

Tags:

Releated Java Articles

Comments

Leave a Reply