使用HttpClient发送http请求,并解析从服务器端返回的数据
来源:互联网 发布:python 程序 编辑:程序博客网 时间:2024/05/01 12:37
使用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;
- publicclass 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 = newbyte[1024];
- byte[] all = newbyte[0];
- int num;
- while ((num = in.read(read)) > 0) {
- byte[] temp = newbyte[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 = newbyte[1024];
- byte[] all = newbyte[0];
- int num;
- while ((num = in.read(read)) > 0) {
- byte[] temp = newbyte[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());
- returnthis.sendGet(urlBuilder.toString());
- }
- }
如果服务器返回的是XML,上面的方法返回的就是xml的字符串,如"<XML><student>......</student></XML>"。在处理xml非常管用。
上面的例子,如果使用第二种或者第三种方法,需要将参数放在Map<String, String>中
- 使用HttpClient发送http请求,并解析从服务器端返回的数据
- 使用HttpClient发送http请求,并解析从服务器端返回的数据
- 使用HttpClient发送http请求,并解析从服务器端返回的数据
- 使用HttpClient发送http请求,并解析从服务器端返回的数据
- 使用HttpClient发送http请求,并解析从服务器端返回的数据
- 在Java中使用HttpURLConnection发送http客户端请求、服务器端返回信息、接受服务器端的信息
- 关于java 发送http json数据格式请求时,服务器端如何接收json数据并解析
- http请求返回并解析json数据
- http请求返回并解析json数据
- 使用handler和Message获取xutils发送POST请求从服务器端返回数据
- 使用HttpClient发送请求并接收返回信息
- Java 使用HttpClient发送GET请求,并获得返回的xml信息
- 爬虫中使用HttpClient发送ajax请求,并处理返回的JSON字符串
- J2SE发送http请求并获取返回数据
- 模拟http发送post请求并返回数据
- Java程序使用httpClient发送Url链接,并获取返回的数据信息
- swift http请求返回json数据并解析
- swift http请求返回json数据并解析
- 输入和输出缓冲区更新(试过成功)
- Codeigniter+PHPExcel中导出数据到Excel文件//http://www.cnblogs.com/jiji262/archive/2012/03/27/2419989.html
- DBCP连接池详解
- 对JSP页面传值中文乱码的简单解决方法
- 门内门外看招聘
- 使用HttpClient发送http请求,并解析从服务器端返回的数据
- Android提高第二十一篇之MediaPlayer播放网络视频
- c++奇怪的搞法汇总
- 如何申请Appkey
- c# 串口的读写
- 使用Vitamio打造自己的Android万能播放器(4)——本地播放(快捷搜索、数据存储)
- PHP导出excel
- 什么是ORM及ORM的详解
- 使用Vitamio打造自己的Android万能播放器(5)——在线播放(播放优酷视频)