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

来源:互联网 发布:windows 10内核源代码 编辑:程序博客网 时间:2024/04/29 04:15
使用Apache的httpclient包可以模拟HTTP请求的发送, get和post均可以。最方便的地方就是请求struts等web框架进行测试,省去了做测试页面的差事。
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>中