使用HttpClient发送http请求,并解析从服务器端返回的数据
2012-07-09 21:33
549 查看
使用Apache的httpclient包可以模拟HTTP请求的发送, get和post均可以。最方便的地方就是请求struts等web框架进行测试,省去了做测试页面的差事。
上面的例子,如果使用第二种或者第三种方法,需要将参数放在Map<String, String>中
import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.Set; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.BufferedHttpEntity; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.message.BasicNameValuePair; public class Client { public String sendGet(String url) throws ClientProtocolException, IOException{ String result = null; HttpClient httpClient = new DefaultHttpClient(); HttpGet get = new HttpGet(url); InputStream in = null; try { HttpResponse response = httpClient.execute(get); HttpEntity entity = response.getEntity(); if (entity != null) { entity = new BufferedHttpEntity(entity); in = entity.getContent(); byte[] read = new byte[1024]; byte[] all = new byte[0]; int num; while ((num = in.read(read)) > 0) { byte[] temp = new byte[all.length + num]; System.arraycopy(all, 0, temp, 0, all.length); System.arraycopy(read, 0, temp, all.length, num); all = temp; } result = new String(all, "UTF-8"); } } finally { if (in != null) in.close(); get.abort(); } return result; } public String sendPost(String url, Map<String, String> params) throws ClientProtocolException, IOException{ String result = null; HttpClient httpClient = new DefaultHttpClient(); HttpPost get = new HttpPost(url); // 创建表单参数列表 List<NameValuePair> qparams = new ArrayList<NameValuePair>(); Set<String> keys = params.keySet(); for (String key : keys) { qparams.add(new BasicNameValuePair(key, params.get(key))); } // 填充表单 get.setEntity(new UrlEncodedFormEntity(qparams,"UTF-8")); HttpResponse response = httpClient.execute(get); HttpEntity entity = response.getEntity(); if (entity != null) { entity = new BufferedHttpEntity(entity); InputStream in = entity.getContent(); byte[] read = new byte[1024]; byte[] all = new byte[0]; int num; while ((num = in.read(read)) > 0) { byte[] temp = new byte[all.length + num]; System.arraycopy(all, 0, temp, 0, all.length); System.arraycopy(read, 0, temp, all.length, num); all = temp; } result = new String(all,"UTF-8"); if (null != in) { in.close(); } } get.abort(); return result; } public String sendGet(String url, Map<String, String> params) throws ClientProtocolException, IOException { Set<String> keys = params.keySet(); StringBuilder urlBuilder = new StringBuilder(url + "?"); for (String key : keys) { urlBuilder.append(key).append("=").append(params.get(key)).append("&"); } urlBuilder.delete(urlBuilder.length() - 1, urlBuilder.length()); return this.sendGet(urlBuilder.toString()); } }如果服务器返回的是XML,上面的方法返回的就是xml的字符串,如"<XML><student>......</student></XML>"。在处理xml非常管用。
上面的例子,如果使用第二种或者第三种方法,需要将参数放在Map<String, String>中
相关文章推荐
- 使用HttpClient发送http请求,并解析从服务器端返回的数据
- 使用HttpClient发送http请求,并解析从服务器端返回的数据
- 使用HttpClient发送http请求,并解析从服务器端返回的数据
- 使用HttpClient发送http请求,并解析从服务器端返回的数据
- 使用HttpClient 发送get、post请求,及其解析xml返回数据
- 使用HttpClient发送HTTP请求(分别通过GET和POST方法发送数据) (3)
- 关于对发送HTTP请求以及解析服务器返回的数据操作的提取到一个公共类中进行封装
- 使用xutils发送POST请求,携带json和图片二进制文件数据获取服务器端返回json数据
- JAVA 使用HttpClient 实现简单发送HTTP请求 无返回结果
- android菜鸟学习笔记25----与服务器端交互(二)解析服务端返回的json数据及使用一个开源组件请求服务端数据
- 使用handler和Message获取xutils发送POST请求从服务器端返回数据
- 使用handler和Message获取xutils发送POST请求从服务器端返回数据
- Java程序使用httpClient发送Url链接,并获取返回的数据信息
- jQuery解析与处理服务器端返回xml格式数据的方法详解
- 使用TCP协议编写一个网络程序,设置服务器端的监听端口是8002,当与客户端建立连接后,服务器端向客户端发送数据“Hello, world”,客户端收到数据后打印输出。
- 使用AFNetworking框架进行post 请求解析返回得数据
- 安卓实训第六天:首先使用AsyncHttpClient来完成网页源代码的显示功能,json数据在服务器端的读取还有安卓上的读取
- Java发送Http请求,解析html返回
- JSON - 使用cJSON 解析Qt通过UDP发送的JSON数据
- Android使用HttpClient以Post、Get请求服务器发送数据的方式(普通和json)