标签:style blog http io ar color os sp for
Httpclient4.x post方法代码:
1 import java.io.BufferedReader; 2 import java.io.IOException; 3 import java.io.InputStream; 4 import java.io.InputStreamReader; 5 import java.io.UnsupportedEncodingException; 6 import java.util.ArrayList; 7 import java.util.List; 8 import org.apache.http.Consts; 9 import org.apache.http.HttpEntity; 10 import org.apache.http.HttpResponse; 11 import org.apache.http.ParseException; 12 import org.apache.http.client.ClientProtocolException; 13 import org.apache.http.client.HttpClient; 14 import org.apache.http.client.entity.UrlEncodedFormEntity; 15 import org.apache.http.client.methods.HttpPost; 16 import org.apache.http.impl.client.DefaultHttpClient; 17 import org.apache.http.message.BasicNameValuePair; 18 import org.apache.http.util.EntityUtils; 19 20 public class test01 { 21 public static void main(String argv[]){ 22 HttpClient client=new DefaultHttpClient(); 23 List<BasicNameValuePair> formparams = new ArrayList<BasicNameValuePair>(); 24 formparams.add(new BasicNameValuePair("money", "xx")); 25 formparams.add(new BasicNameValuePair("tele_num", "xxxxxxxxxxx")); 26 UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, Consts.UTF_8); 27 HttpPost httppost = new HttpPost("http://xxxx.xxxxxx.xx/xxx/xxxxxx"); 28 httppost.setEntity(entity); 29 30 HttpResponse response = null; 31 try { 32 response = client.execute(httppost); 33 } catch (ClientProtocolException e) { 34 // TODO Auto-generated catch block 35 e.printStackTrace(); 36 } catch (IOException e) { 37 // TODO Auto-generated catch block 38 e.printStackTrace(); 39 } 40 //打印服务器返回的状态 41 42 System.out.println(response.getStatusLine()); 43 HttpEntity Entity = response.getEntity(); 44 //打印结果页面 45 46 try { 47 System.out.println("Response is :"+EntityUtils.toString(Entity)); 48 } catch (ParseException e1) { 49 // TODO Auto-generated catch block 50 e1.printStackTrace(); 51 } catch (IOException e1) { 52 // TODO Auto-generated catch block 53 e1.printStackTrace(); 54 } 55 if(Entity!=null){ 56 System.out.println("The Response length is :"+Entity.getContentLength()); 57 } 58 } 59 }
Httpclient3.x post方法代码:
1 import java.io.IOException; 2 import java.io.UnsupportedEncodingException; 3 import org.apache.commons.httpclient.HttpClient; 4 import org.apache.commons.httpclient.HttpException; 5 import org.apache.commons.httpclient.NameValuePair; 6 import org.apache.commons.httpclient.methods.PostMethod; 7 8 9 public class test02 { 10 public static void main(String argv[]){ 11 HttpClient client = new HttpClient(); 12 client.getHostConfiguration().setHost("xxxx.xxxxxx.xx", 80, "http"); 13 PostMethod post = new PostMethod("http://xxxx.xxxxxx.xx/xxx/xxxxxx"); 14 NameValuePair money = new NameValuePair("money","xx"); 15 NameValuePair tele_num = new NameValuePair("tele_num", "xxxxxxxxxxx"); 16 post.setRequestBody(new NameValuePair[] { money, tele_num}); 17 18 try { 19 client.executeMethod(post); 20 } catch (HttpException e) { 21 // TODO Auto-generated catch block 22 e.printStackTrace(); 23 } catch (IOException e) { 24 // TODO Auto-generated catch block 25 e.printStackTrace(); 26 } 27 //打印服务器返回的状态 28 System.out.println("服务器返回的状态是: "+post.getStatusLine()); 29 //打印结果页面 30 String response = null; 31 try { 32 response = new String(post.getResponseBodyAsString().getBytes("UTF-8")); 33 } catch (UnsupportedEncodingException e) { 34 // TODO Auto-generated catch block 35 e.printStackTrace(); 36 } catch (IOException e) { 37 // TODO Auto-generated catch block 38 e.printStackTrace(); 39 } 40 //打印返回的信息 41 System.out.println("返回的信息是: "+response); 42 post.releaseConnection(); 43 44 } 45 }
附参考:http://hc.apache.org/httpcomponents-client-ga/tutorial/html/
JAVA Httpclient3.x与Httpclient4.x代码对比(post方法)
标签:style blog http io ar color os sp for
原文地址:http://www.cnblogs.com/sky-of-chuanqingchen/p/4164903.html