HttpClient入门
来源:互联网 发布:一流程序员算法 编辑:程序博客网 时间:2024/06/06 13:57
HttpClient 是apache 组织下的一个用于处理HTTP请求和响应的开源工具。它不是一个浏览器,也不处理客户端缓存等浏览器的功能。它只是一个类库!它在JDK 的基本类库基础上做了更好的封装!HttpClient 项目依赖于HttpCore(处理核心的HTTP 协议)、commons-codec(处理与编码有关的问题的项目)和commons-logging(处理与日志记录有关问题的项目)。
如果你希望能够通过HttpClient 向服务器上传文件等与multipart编码类型有关的请求,以及其它复杂的MIME 类型,那么,你需要另外一个依赖包:HttpMime(它是专门处理与MIME 类型有关问题的项目),在下载的HttpClient 包中(下载地址为:http://hc.apache.org/downloads.cgi)已经包含了HttpMime。
1. HttpClient的范围
基于HttpCore[http://hc.apache.org/httpcomponents-core/index.html]的客户端HTTP运输实现库
基于经典(阻塞)I/O
内容无关
2. 什么是HttpClient不能做的
HttpClient 不是一个浏览器。它是一个客户端的HTTP通信实现库。HttpClient的目标是发送和接收HTTP报文。HttpClient不会去缓存内容,执行嵌入在HTML页面中的javascript代码,猜测内容类型,重新格式化请求/重定向URI,或者其它和HTTP运输无关的功能。
例子:
import java.util.ArrayList;import java.util.List;import org.apache.http.HttpEntity;import org.apache.http.HttpResponse;import org.apache.http.NameValuePair;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.impl.client.DefaultHttpClient;import org.apache.http.message.BasicNameValuePair;import org.apache.http.util.EntityUtils;public class QuickStart { public static void main(String[] args) throws Exception { DefaultHttpClient httpclient = new DefaultHttpClient(); HttpGet httpGet = new HttpGet("http://targethost/homepage"); HttpResponse response1 = httpclient.execute(httpGet); // The underlying HTTP connection is still held by the response object // to allow the response content to be streamed directly from the network socket. // In order to ensure correct deallocation of system resources // the user MUST either fully consume the response content or abort request // execution by calling HttpGet#releaseConnection(). try { System.out.println(response1.getStatusLine()); HttpEntity entity1 = response1.getEntity(); // do something useful with the response body // and ensure it is fully consumed EntityUtils.consume(entity1); } finally { httpGet.releaseConnection(); } HttpPost httpPost = new HttpPost("http://targethost/login"); List <NameValuePair> nvps = new ArrayList <NameValuePair>(); nvps.add(new BasicNameValuePair("username", "vip")); nvps.add(new BasicNameValuePair("password", "secret")); httpPost.setEntity(new UrlEncodedFormEntity(nvps)); HttpResponse response2 = httpclient.execute(httpPost); try { System.out.println(response2.getStatusLine()); HttpEntity entity2 = response2.getEntity(); // do something useful with the response body // and ensure it is fully consumed EntityUtils.consume(entity2); } finally { httpPost.releaseConnection(); } }}
- HttpClient入门
- HttpClient入门
- httpClient入门
- HTTPClient入门
- HttpClient入门
- HttpClient入门
- HttpClient入门
- HttpClient入门
- HttpClient入门
- HttpClient入门
- HttpClient入门
- HttpClient入门
- HttpClient入门
- HttpClient入门
- HttpClient入门
- HttpClient入门 .
- HttpClient入门
- HttpClient入门
- 在android应用底部显示菜单
- Daily-C-Study(7):C语言const关键字
- 记录锁 详解
- Struts1.x中ActionForm的应用探究
- javascript技巧
- HttpClient入门
- flash导出gif动画
- Eclipse注释模板自动显示作者姓名
- 广州传智播客.Net训练营六期开班典礼
- 互联网QA工作方式
- 我的第一个MFC的ArcGIS Engine程序
- LinkedList源码分析
- 带着项目学PHP第二讲 - 如何切换ecshop模板
- LINUX-C成长之路(一):LINUX下C编程概要