HttpClient介绍
来源:互联网 发布:网络招商推广语言 编辑:程序博客网 时间:2024/05/22 12:23
一、HttpClient介绍
HttpClient是用来模拟HTTP请求的,其实实质就是把HTTP请求模拟后发给Web服务器;
Android已经集成了HttpClient,因此可以直接使用;
注:此处HttpClient代码不只可以适用于Android,也可适用于一般的Java程序;
HTTP GET核心代码:
(1)DefaultHttpClient client = new DefaultHttpClient();
(2)HttpGet get = new HttpGet(String url);//此处的URL为http://..../path?arg1=value&....argn=value
(3)HttpResponse response = client.execute(get); //模拟请求
(4)int code = response.getStatusLine().getStatusCode();//返回响应码
(5)InputStream in = response.getEntity().getContent();//服务器返回的数据
HTTP POST核心代码:
(1)DefaultHttpClient client = new DefaultHttpClient();
(2)BasicNameValuePair pair = new BasicNameValuePair(String name,String value);//创建一个请求头的字段,比如content-type,text/plain
(3)UrlEncodedFormEntity entity = new UrlEncodedFormEntity(List<NameValuePair> list,String encoding);//对自定义请求头进行URL编码
(4)HttpPost post = new HttpPost(String url);//此处的URL为http://..../path
(5)post.setEntity(entity);
(6)HttpResponse response = client.execute(post);
(7)int code = response.getStatusLine().getStatusCode();
(8)InputStream in = response.getEntity().getContent();//服务器返回的数据
其中
UrlEncodedFormEntity,直接继承自StringEntity
有两个构造方法:
1. UrlEncodedFormEntity(List<? extends NameValuePair> parameters, String encoding)
2. UrlEncodedFormEntity(List<? extends NameValuePair> parameters)
第二个方法将使用默认编码,即ISO-8859-1
例如:
/*
* NameValuePair代表一个HEADER,List<NameValuePair>存储全部的头字段
* UrlEncodedFormEntity类似于URLEncoder语句进行URL编码
* HttpPost类似于HTTP的POST请求
* client.execute()类似于发出请求,并返回Response
*/
public static String AccessWebUtil(HashMap<String, String> param, String url) {
String content = "";
HttpPost request = new HttpPost(url);
List<NameValuePair> paramers = new LinkedList<NameValuePair>();
for (Map.Entry<String, String> entry : param.entrySet()) {
paramers.add(new BasicNameValuePair(entry.getKey(), entry
.getValue()));
}
UrlEncodedFormEntity entity = null;
try {
entity = new UrlEncodedFormEntity(paramers, "utf-8");
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
BasicHttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams, 1000 * 60);
HttpConnectionParams.setSoTimeout(httpParams, 1000 * 60);
request.setEntity(entity);
HttpClient httpClient = new DefaultHttpClient(httpParams);
BufferedReader in = null;
try {
// 执行请求参数项
HttpResponse response = httpClient.execute(request);
// 判断是否请求成功
// System.out.println("temp=="+response.getStatusLine().getStatusCode()
// );
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
// 获得响应信息
in = new BufferedReader(new InputStreamReader(response
.getEntity().getContent()));
StringBuffer string = new StringBuffer("");
String lineStr = "";
while ((lineStr = in.readLine()) != null) {
string.append(lineStr + "\n");
}
content = string.toString();
return content;
} else {
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
// 释放网络连接资源
httpClient.getConnectionManager().shutdown();
}
return "";
}
- HTTPClient介绍
- httpclient介绍
- HttpClient介绍
- HttpClient介绍
- httpclient介绍
- HttpClient介绍
- HttpClient介绍
- HttpClient介绍
- HttpClient介绍
- HttpClient介绍
- Android HttpClient介绍
- HttpClient,HttpUrlConectTion,Socket介绍
- Android入门:HttpClient介绍
- HttpGet/HttpPost/HttpClient介绍
- httpclient 简单介绍使用方法
- HttpClient 详细介绍
- HTTPClient简单用法介绍
- Httpclient介绍及使用
- 华图网校远程课程5大主力名师
- UBUNTU下如何开启SSHD服务
- PHP文件包含漏洞攻防实战
- UCGUI不显示窗口(WM)
- http://blog.csdn.net/shadowsniper/article/details/5750580
- HttpClient介绍
- jQuery获取CSS样式中的颜色值的问题
- python应用领域介绍 -- (python 真的很强大)
- NuttX 启动流程
- 高效开发Android App的10个建议
- Maven3.0.3的环境变量配置
- 读后感:企业软件难做难用,主要责任不全在软件开发商。
- .NET如何避免内存泄漏
- IOS中定时器NSTimer的开启与关闭