1.1 执行请求
HttpClient大部分必要的功能是执行HTTP方法。HTTP方法的执行涉及了一个或者多个HTTP请求和响应的交互,通常会在HttpClient内部处理。要求用户提供请求对象执行,并且期盼HttpClient传输请求给目标服务器然后返回相应的响应对象,如果执行不成功则抛出异常。
非常自然的,HttpClient API的main入口点是HttpClient接口,它定义了上述描述内容。
下面使用最简单的形式描述请求执行的例子:
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet("http://www.google.com");
HttpResponse response = httpClient.execute(httpGet);
HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream instream = entity.getContent();
int l;
byte[] tmp = new byte[2048];
while ((l = instream.read(tmp)) != -1) {
StringBuffer sb = new StringBuffer();
for(byte element : tmp){
sb.append(String.valueOf(element));
}
System.out.println(sb.toString());
}
}
}
分享到:
相关推荐
HttpClient Tutorial
httpclient 指南 包括了详细的调用和常用代码 The Hyper-Text Transfer Protocol (HTTP) is perhaps the most significant protocol used on the Internet today. Web services, network-enabled appliances and the...
httpclient-tutorial-simplified-chinese.pdf
apache httpclient 开发指导文档4.5版本,内容比较全面,开发过程中值得借鉴,文档中各种应用场景已经覆盖到了。
httpclient-tutorial.pdf httpclient-tutorial.pdf httpclient-tutorial.pdf
HttpClient seeks to fill this void by providing an efficient, up-to-date, and feature-rich package implementing the client side of the most recent HTTP standards and recommendations. Designed for ...
httpclient-tutorial开发指导文档
httpclient-tutorial网络爬虫 使你生活更加便利 哈哈
网上找的,共同分享! HttpClient4.0.3 Tutorial中文翻译!
HttpClient4_Tutorial_API_Doc_En+Cn.rar
apache httpclient 4 tutorial
资源包括httpclient-4.1.3.jar、httpclient-cache-4.1.3.jar、httpcore-4.1.4.jar、httpmime-4.1.3.jar、commons-logging-1.1.1.jar、commons-codec-1.4.jar6个jar包,tutorial,javadoc和examples,资源来自hc....
含tutorial,commons-logging-1.1.1, commons-codec-1.4-bin.zip, httpcomponents-client-4.0.3-bin.zip, httpcomponents-core-4.1-bin.zip
HttpClient 是 Apache Jakarta Common 下的子项目,可以用来提供高效的、最新的、功能丰富的支持 HTTP 协议的客户端编程工具包,...本文首先介绍 HTTPClient,然后根据作者实际工作经验给出了一些常见问题的解决方法。