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

来源:互联网 发布:python 程序 编辑:程序博客网 时间:2024/05/01 12:37

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

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

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


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

原创粉丝点击