`

Httpclient4.0 功能封装

阅读更多
http://hc.apache.org/httpcomponents-client/examples.html

http://www.iteye.com/topic/192393

4.0中HttpClient不再是一个类,而是一个接口了,

4.0跟3.X的版本相比, 不但底层做了修改,高层架构也做了调整。 并且在Android项目中应用到了4.0, 应该比之前的版本更加稳定。

/*
 * ====================================================================
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 * ====================================================================
 *
 * This software consists of voluntary contributions made by many
 * individuals on behalf of the Apache Software Foundation.  For more
 * information on the Apache Software Foundation, please see
 * <http://www.apache.org/>.
 *
 */

package org.apache.http.examples.client;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

/**
 * 第二个例子 This example demonstrates the recommended way of using API to make sure
 * the underlying connection gets released back to the connection manager.
 */
public class ClientConnectionRelease {

	public final static void main(String[] args) throws Exception {
		HttpClient httpclient = new DefaultHttpClient();

		HttpGet httpget = new HttpGet("http://www.apache.org/");
		HttpResponse response = httpclient.execute(httpget);
		
		HttpEntity entity = response.getEntity();
		try {
			StringBuffer sb = processEntity(entity);
			System.out.println(sb);
		} catch (RuntimeException ex) {
			httpget.abort();
			throw ex;

		}
		httpclient.getConnectionManager().shutdown();
	}

	/**
	 * 处理一个实体
	 * @param entity
	 */
	private static StringBuffer processEntity(HttpEntity entity) 
	{
		StringBuffer sb = new StringBuffer();
		InputStreamReader iReader = null;
		try {
		        InputStream inputStream = entity.getContent();
			iReader = new InputStreamReader(inputStream);
			BufferedReader reader = new BufferedReader(iReader);
			String line = null;
			// 用reader.ready()是不行的
			while ((line = reader.readLine()) != null) {
			sb.append(line + "\r\n");
			}
		      } 
                      catch (Exception ex) {
			    ex.printStackTrace();
		      } finally 
                       {
				try {
					iReader.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			return sb;
		}

}


分享到:
评论

相关推荐

    Spring for Android 2 中文 参考手册 中文文档

    在Android平台上,标准的J2SE设施通过SimpleClientHttpRequestFactory进行封装,而HttpClient则通过HttpComponentsAndroidClientHttpRequestFactory封装。建议在Android 2.3(Gingerbread)及以上版本使用J2SE工具,...

    httpcomponents-core-4.0-bin.zip

    HttpComponents Core 4.0 设计为模块化,可以与其他Apache HttpComponents项目(如HttpClient和HttpServer)无缝集成,实现完整的HTTP客户端和服务器功能。同时,它的API设计易于扩展,允许开发者自定义处理器、拦截...

    HttpHelper万能框架V1.0-.net4.0版

    HTTPHelper框架则封装了这些复杂的交互过程,让开发者可以专注于业务逻辑,而非底层网络通信细节。 该HttpHelper万能框架V1.0的核心特性包括: 1. **易用性**:HttpHelper框架设计简洁,API接口直观,使得开发者...

    c# http协议,实现get或post发送请求 并返回内容

    using (var httpClient = new HttpClient()) { using (var response = await httpClient.GetAsync(url)) { response.EnsureSuccessStatusCode(); return await response.Content.ReadAsStringAsync(); } } }...

    system.net.http.dll

    它们封装了请求头、请求方法、URL、请求内容以及响应状态码、响应头、响应内容等信息。在进行复杂HTTP交互时,开发者通常会直接操作这两个对象来定制请求和处理响应。 3. **HttpContent类**:`HttpContent`是表示...

    httphelper类

    在.NET框架的不同版本(如2.0和4.0)中,`HttpHelper`可能有略微不同的实现,但核心功能保持一致,即提供对HTTP协议的抽象和封装。 以下是对`HttpHelper`类主要知识点的详细说明: 1. **发起HTTP请求** `...

    httpasyncclient jar包

    通过httpasyncclient-4.0-beta4.jar,开发者能够构建高性能的网络应用,实现高效的HTTP请求处理,同时还能享受到Apache HttpClient的其他高级特性,如连接池、多路复用、压缩支持等。在实际开发中,结合使用这些组件...

    http源代码

    HttpClient是HttpCore的重要组成部分,提供了完整的HTTP客户端功能。它允许用户自定义请求头、处理重定向、管理Cookie等,同时还支持各种HTTP方法如GET、POST等。 4. **HttpConnection接口** HttpConnection接口...

    WeatherReport

    另外,`SoapHttpClientProtocol`(在.NET Framework 4.0及以后版本中被`HttpClient`类取代)是调用SOAP Web服务的标准方式。 1. **获取天气预报** 要调用提供天气预报的Web Service,你需要知道其WSDL(Web ...

    vS2008C#写的http下载器

    为了保持代码的可读性和可维护性,我们需要合理地组织项目结构,将核心功能如HTTP请求、文件处理等封装成独立的类或方法。 以上是构建一个基于VS2008和C#的HTTP断点续传下载器所涉及的关键知识点。通过掌握这些...

    java调用webservice的axis2.jar包

    8. httpcore-4.0.jar:Apache HTTP Core库,提供了HTTP协议的基本实现,是HttpClient库的基础。 9. axiom-dom-1.2.10.jar:AXIOM的DOM实现,用于处理DOM树结构。 三、调用WebService流程 1. 获取WSDL:首先,你需要...

    .Net整合Json实现REST服务客户端的方法详解

    前言 本文主要给大家介绍了关于.Net整合Json实现REST服务客户端的相关内容,分享出来供大家参考...1. HttpClientUtil.cs 封装REST方法 using Newtonsoft.Json; using System; using System.Collections.Generic; u

    使用C#控制SqueezeBox服务器(VS2010)-第1部分

    总的来说,通过结合C# 4.0的异步编程特性和对SqueezeBox服务器API的利用,我们可以构建功能丰富的音乐管理应用,使用户能从任何地方控制他们的音乐库。这不仅是一个学习C#和HTTP通信的好项目,也是提升软件开发技能...

    ASP_NET Web服务高级编程(上)

    在.NET环境中,可以使用SoapHttpClientProtocol类(在.NET Framework早期版本)或者HttpClient类(在.NET Framework 4.0及更高版本)来创建客户端代理,自动处理SOAP请求和响应。 ASP.NET Web服务不仅支持SOAP协议...

    多线程采集C#源代码

    此外,`Task`和`async/await`是C# 4.0引入的异步编程模型,它们在多线程采集中有广泛的应用。`Task`代表一个异步操作,而`async/await`则简化了异步编程的语法,使代码更加直观和易于理解。通过异步编程,我们可以让...

    个人系统截图

    - 封装:通过访问修饰符(public, private, protected等)来隐藏数据,防止外部代码直接访问,实现数据安全。 - 继承:子类继承父类的属性和方法,实现代码复用和扩展。 - 多态:同一操作作用于不同的对象,可以...

    resttemplate

    `RestTemplate`是Spring框架中的一个核心组件,主要用于发送HTTP请求和处理响应,它是Spring对Java的HttpURLConnection、Apache HttpClient以及 Ning Async Http Client等HTTP客户端的抽象封装,提供了丰富的API来...

    JAVA上百实例源码以及开源项目源代码

    Java 源码包 Applet钢琴模拟程序java源码 2个目标文件,提供基本的音乐编辑功能。编辑音乐软件的朋友,这款实例会对你有所帮助。 Calendar万年历 1个目标文件 EJB 模拟银行ATM流程及操作源代码 6个目标文件,EJB来...

Global site tag (gtag.js) - Google Analytics