码迷,mamicode.com
首页 > Web开发 > 详细

httpclient post请求例子(无参数名与带参数名的例子)

时间:2015-08-05 12:42:47      阅读:240      评论:0      收藏:0      [点我收藏+]

标签:

版本:4.1

  • 带参数名的情况
    HttpClient httpClient = new DefaultHttpClient(); 
HttpPost httpPost = new HttpPost(url); // httpPost.setHeader("Accept-Encoding", "gzip,deflate");//表示返回的数据是压缩的zip格式 String postParam = "";//请求的参数内容 List<NameValuePair> nvps = new ArrayList<NameValuePair>(); nvps.add(new BasicNameValuePair("data", postParam)); httpPost.setEntity(new UrlEncodedFormEntity(nvps,"utf-8")); HttpResponse response = httpClient.execute(httpPost); HttpEntity entity = response.getEntity(); if (response.getStatusLine().getStatusCode() == 200) {if (entity.getContentEncoding().toString().equalsIgnoreCase("Content-Encoding: gzip")) { response.setEntity(new GzipDecompressingEntity(response.getEntity())); //对zip进行解压 entity = response.getEntity(); } String responseContent = EntityUtils.toString(entity); System.out.println("responseContent: \n" + responseContent); } }
  • 无参数名的情况

 

   HttpClient httpClient = new DefaultHttpClient(); 
    HttpPost httpPost = new HttpPost(url);
//  httpPost.setHeader("Accept-Encoding", "gzip,deflate");//表示返回的数据是压缩的zip格式
    String postParam = "";//请求的参数内容 
    StringEntity paramEntity = new StringEntity(postParam);//无参数名,只是参数内容
    httpPost.setEntity(paramEntity); 
    HttpResponse response = httpClient.execute(httpPost);    
    HttpEntity entity = response.getEntity();
    if (response.getStatusLine().getStatusCode() == 200) {if (entity.getContentEncoding().toString().equalsIgnoreCase("Content-Encoding: gzip")) {
                response.setEntity(new GzipDecompressingEntity(response.getEntity())); //对zip进行解压
                entity = response.getEntity();
           }
           String responseContent = EntityUtils.toString(entity);
           System.out.println("responseContent: \n" + responseContent); 
   }
}


 

  • httpEntity的类结构图

技术分享

 

httpclient post请求例子(无参数名与带参数名的例子)

标签:

原文地址:http://www.cnblogs.com/aisam/p/4704164.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!