Java 微信开发(三) 封装httpclient get post 请求

直接贴代码

 

[java] view plain copy

  1. package com.website.commons.web.utils;  
  2.   
  3. import java.io.IOException;  
  4.   
  5. import org.apache.http.HttpEntity;  
  6. import org.apache.http.HttpResponse;  
  7. import org.apache.http.HttpStatus;  
  8. import org.apache.http.client.ClientProtocolException;  
  9. import org.apache.http.client.HttpClient;  
  10. import org.apache.http.client.methods.HttpGet;  
  11. import org.apache.http.client.methods.HttpPost;  
  12. import org.apache.http.entity.StringEntity;  
  13. import org.apache.http.impl.client.DefaultHttpClient;  
  14. import org.apache.http.util.EntityUtils;  
  15. import org.json.JSONObject;  
  16.   
  17. public class HttpClientUtils {  
  18.   
  19.     public JSONObject doGet(String url) {  
  20.         // 指定get请求  
  21.         HttpGet httpGet = new HttpGet(url);  
  22.         // 创建httpclient  
  23.         HttpClient httpClient = new DefaultHttpClient();  
  24.         // 发送请求  
  25.         HttpResponse httpResponse;  
  26.         //返回的json  
  27.         JSONObject jsonObject = null;  
  28.         try {  
  29.             httpResponse = httpClient.execute(httpGet);  
  30.             // 验证请求是否成功  
  31.             if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {  
  32.                 // 得到请求响应信息  
  33.                 String str = EntityUtils.toString(httpResponse.getEntity(),  
  34.                         "utf-8");  
  35.                 // 返回json  
  36.                 jsonObject = new JSONObject(str);  
  37.             }  
  38.         } catch (ClientProtocolException e) {  
  39.             // TODO Auto-generated catch block  
  40.             e.printStackTrace();  
  41.         } catch (IOException e) {  
  42.             // TODO Auto-generated catch block  
  43.             e.printStackTrace();  
  44.         }  
  45.         return jsonObject;  
  46.     }  
  47.       
  48.       
  49.     public JSONObject doPost(String url, JSONObject jsonData) {  
  50.         // 指定Post请求  
  51.         HttpPost httpPost = new HttpPost(url);  
  52.         // 创建httpclient  
  53.         HttpClient httpClient = new DefaultHttpClient();  
  54.         // 发送请求  
  55.         HttpResponse httpResponse;  
  56.         // 返回的json  
  57.         JSONObject jsonObject = null;  
  58.         // 封装post请求数据  
  59.         StringEntity entity = new StringEntity(jsonData.toString(), "utf-8");  
  60.         httpPost.setEntity(entity);  
  61.         try {  
  62.             // 发送请求  
  63.             httpResponse = httpClient.execute(httpPost);  
  64.             // 判断请求是否成功  
  65.             if(httpResponse.getStatusLine().getStatusCode()==HttpStatus.SC_OK){  
  66.                 // 得到请求响应信息  
  67.                 String str = EntityUtils.toString(httpResponse.getEntity(), "utf-8");  
  68.                 // 返回json  
  69.                 jsonObject = new JSONObject(str);  
  70.             }  
  71.         } catch (ClientProtocolException e) {  
  72.             // TODO Auto-generated catch block  
  73.             e.printStackTrace();  
  74.         } catch (IOException e) {  
  75.             // TODO Auto-generated catch block  
  76.             e.printStackTrace();  
  77.         }  
  78.         return jsonObject;  
  79.     }  
  80.       
  81. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值