Httpclient4.x Post Method Code:
1 ImportJava.io.BufferedReader;2 Importjava.io.IOException;3 ImportJava.io.InputStream;4 ImportJava.io.InputStreamReader;5 Importjava.io.UnsupportedEncodingException;6 Importjava.util.ArrayList;7 Importjava.util.List;8 Importorg.apache.http.Consts;9 Importorg.apache.http.HttpEntity;Ten ImportOrg.apache.http.HttpResponse; One Importorg.apache.http.ParseException; A Importorg.apache.http.client.ClientProtocolException; - Importorg.apache.http.client.HttpClient; - Importorg.apache.http.client.entity.UrlEncodedFormEntity; the ImportOrg.apache.http.client.methods.HttpPost; - Importorg.apache.http.impl.client.DefaultHttpClient; - ImportOrg.apache.http.message.BasicNameValuePair; - Importorg.apache.http.util.EntityUtils; + - Public classtest01 { + Public Static voidMain (String argv[]) { AHttpClient client=Newdefaulthttpclient (); atList<basicnamevaluepair> Formparams =NewArraylist<basicnamevaluepair>(); -Formparams.add (NewBasicnamevaluepair ("Money", "XX")); -Formparams.add (NewBasicnamevaluepair ("Tele_num", "xxxxxxxxxxx")); -urlencodedformentity entity =Newurlencodedformentity (Formparams, consts.utf_8); -HttpPost HttpPost =NewHttpPost ("Http://xxxx.xxxxxx.xx/xxx/xxxxxx"); - httppost.setentity (entity); in -HttpResponse response =NULL; to Try { +Response =Client.execute (httppost); -}Catch(clientprotocolexception e) { the //TODO auto-generated Catch block * e.printstacktrace (); $}Catch(IOException e) {Panax Notoginseng //TODO auto-generated Catch block - e.printstacktrace (); the } + //status returned by the print server A the System.out.println (Response.getstatusline ()); +Httpentity Entity =response.getentity (); - //Print results page $ $ Try { -System.out.println ("Response is:" +entityutils.tostring (Entity)); -}Catch(ParseException E1) { the //TODO auto-generated Catch block - e1.printstacktrace ();Wuyi}Catch(IOException E1) { the //TODO auto-generated Catch block - e1.printstacktrace (); Wu } - if(entity!=NULL){ AboutSystem.out.println ("The Response length is:" +entity.getcontentlength ()); $ } - } -}
Httpclient3.x Post Method Code:
1Import java.io.IOException;2 Importjava.io.UnsupportedEncodingException;3 Importorg.apache.commons.httpclient.HttpClient;4 Importorg.apache.commons.httpclient.HttpException;5 ImportOrg.apache.commons.httpclient.NameValuePair;6 ImportOrg.apache.commons.httpclient.methods.PostMethod;7 8 9 Public classtest02 {Ten Public Static voidMain (String argv[]) { OneHttpClient client =NewHttpClient (); AClient.gethostconfiguration (). Sethost ("xxxx.xxxxxx.xx", "http"); -Postmethod post =NewPostmethod ("Http://xxxx.xxxxxx.xx/xxx/xxxxxx"); -Namevaluepair Money =NewNamevaluepair ("Money", "XX"); theNamevaluepair Tele_num =NewNamevaluepair ("Tele_num", "xxxxxxxxxxx"); -Post.setrequestbody (Newnamevaluepair[] {money, tele_num}); - - Try { + Client.executemethod (POST); -}Catch(HttpException e) { + //TODO auto-generated Catch block A e.printstacktrace (); at}Catch(IOException e) { - //TODO auto-generated Catch block - e.printstacktrace (); - } - //status returned by the print server -System.out.println ("The status returned by the server is:" +post.getstatusline ()); in //Print results page -String response =NULL; to Try { +Response =NewString (Post.getresponsebodyasstring (). GetBytes ("UTF-8")); -}Catch(unsupportedencodingexception e) { the //TODO auto-generated Catch block * e.printstacktrace (); $}Catch(IOException e) {Panax Notoginseng //TODO auto-generated Catch block - e.printstacktrace (); the } + //Print the returned information ASYSTEM.OUT.PRINTLN ("The information returned is:" +response); the post.releaseconnection (); + - } $}
Attached reference: http://hc.apache.org/httpcomponents-client-ga/tutorial/html/
JAVA httpclient3.x vs. httpclient4.x code (POST method)