使用HttpClient发送http请求,并解析从服务器端返回的数据

使用Apache的httpclient包可以模拟HTTP请求的发送, get和post均可以。最方便的地方就是请求struts等web框架进行测试,省去了做测试页面的差事。

[java]  view plain copy print ?
  1. import java.io.IOException;  
  2. import java.io.InputStream;  
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5. import java.util.Map;  
  6. import java.util.Set;  
  7.   
  8. import org.apache.http.HttpEntity;  
  9. import org.apache.http.HttpResponse;  
  10. import org.apache.http.NameValuePair;  
  11. import org.apache.http.client.ClientProtocolException;  
  12. import org.apache.http.client.HttpClient;  
  13. import org.apache.http.client.entity.UrlEncodedFormEntity;  
  14. import org.apache.http.client.methods.HttpGet;  
  15. import org.apache.http.client.methods.HttpPost;  
  16. import org.apache.http.entity.BufferedHttpEntity;  
  17. import org.apache.http.impl.client.DefaultHttpClient;  
  18. import org.apache.http.message.BasicNameValuePair;  
  19.   
  20. public class Client {  
  21.       
  22.     public String sendGet(String url) throws ClientProtocolException, IOException{  
  23.         String result = null;  
  24.         HttpClient httpClient = new DefaultHttpClient();  
  25.         HttpGet get = new HttpGet(url);  
  26.         InputStream in = null;  
  27.         try {  
  28.             HttpResponse response = httpClient.execute(get);  
  29.             HttpEntity entity = response.getEntity();  
  30.             if (entity != null) {  
  31.                 entity = new BufferedHttpEntity(entity);  
  32.                 in = entity.getContent();  
  33.                 byte[] read = new byte[1024];  
  34.                 byte[] all = new byte[0];  
  35.                 int num;  
  36.                 while ((num = in.read(read)) > 0) {  
  37.                     byte[] temp = new byte[all.length + num];  
  38.                     System.arraycopy(all, 0, temp, 0, all.length);  
  39.                     System.arraycopy(read, 0, temp, all.length, num);  
  40.                     all = temp;  
  41.                 }  
  42.                 result = new String(all, "UTF-8");  
  43.             }  
  44.         } finally {  
  45.             if (in != null) in.close();  
  46.             get.abort();  
  47.         }  
  48.           
  49.         return result;  
  50.     }  
  51.       
  52.     public String sendPost(String url, Map<String, String> params) throws ClientProtocolException, IOException{  
  53.         String result = null;  
  54.         HttpClient httpClient = new DefaultHttpClient();  
  55.           
  56.         HttpPost get = new HttpPost(url);  
  57.           
  58.         // 创建表单参数列表    
  59.         List<NameValuePair> qparams = new ArrayList<NameValuePair>();   
  60.         Set<String> keys = params.keySet();  
  61.         for (String key : keys) {  
  62.             qparams.add(new BasicNameValuePair(key, params.get(key)));  
  63.         }  
  64.           
  65.         // 填充表单    
  66.         get.setEntity(new UrlEncodedFormEntity(qparams,"UTF-8"));  
  67.           
  68.         HttpResponse response = httpClient.execute(get);  
  69.         HttpEntity entity = response.getEntity();  
  70.         if (entity != null) {  
  71.             entity = new BufferedHttpEntity(entity);  
  72.               
  73.             InputStream in = entity.getContent();  
  74.             byte[] read = new byte[1024];  
  75.             byte[] all = new byte[0];  
  76.             int num;  
  77.             while ((num = in.read(read)) > 0) {  
  78.                 byte[] temp = new byte[all.length + num];  
  79.                 System.arraycopy(all, 0, temp, 0, all.length);  
  80.                 System.arraycopy(read, 0, temp, all.length, num);  
  81.                 all = temp;  
  82.             }  
  83.             result = new String(all,"UTF-8");  
  84.             if (null != in) {  
  85.                 in.close();  
  86.             }  
  87.         }  
  88.         get.abort();  
  89.           
  90.         return result;  
  91.     }  
  92.       
  93.     public String sendGet(String url, Map<String, String> params) throws ClientProtocolException, IOException {  
  94.         Set<String> keys = params.keySet();  
  95.         StringBuilder urlBuilder = new StringBuilder(url + "?");  
  96.         for (String key : keys) {  
  97.             urlBuilder.append(key).append("=").append(params.get(key)).append("&");  
  98.         }  
  99.         urlBuilder.delete(urlBuilder.length() - 1, urlBuilder.length());  
  100.         return this.sendGet(urlBuilder.toString());  
  101.     }  
  102. }  


如果服务器返回的是XML,上面的方法返回的就是xml的字符串,如"<XML><student>......</student></XML>"。在处理xml非常管用。


上面的例子,如果使用第二种或者第三种方法,需要将参数放在Map<String, String>中

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值