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

时间:2015-08-05 12:42:24   收藏:0   阅读:1222

版本: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); 
   }
}


 

技术分享

 

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

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!