您的位置:首页 > 理论基础 > 计算机网络

HttpURLConnection与 HttpClient 区别

2016-07-21 10:00 561 查看
HttpClient

Apache公司提供的库,提供高效的、最新的、功能丰富的支持HTTP协议工具包,支持HTTP协议最新的版本和建议,是个很不错的开源框架,封装了http的请求,参数,内容体,响应等,拥有众多API。

HttpURLConnection

Sun公司提供的库,也是Java的标准类库java.net中的一员,但这个类什么都没封装,用起来很原始,若需要高级功能,则会显得不太方便,比如重访问的自定义,会话和cookie等一些高级功能。

用法

这两种方式都支持HTTPS协议,以流的形式进行上传和下载,配置超时时间,IPV6,连接池等功能。

HttpClient的用法

一、执行请求

HttpClient最重要的功能是执行HTTP方法。一个HTTP方法的执行包含一个或多个HTTP请求/HTTP响应交流,通常由HttpClient的内部来处理。用户提供一个请求对象,HttpClient发送请求到目标服务器,希望服务器返回一个相应的响应对象,或者抛出一个异常(执行失败)。

一个简单的请求执行过程的示例:

HttpClient httpclient = new DefaultHttpClient();

HttpGet httpget = new HttpGet(“htt p:/ /localhost/”);

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) {

}

}

二、用工具封装Get请求

对比下面两段代码,发现用httpClient提供的方法更具可读性

1

2

HttpGet httpget = new HttpGet(

“htt p:/ /ww w.g oogl e.c om/search?hl=en&q=httpclient&btnG=Google+Search&aq=f&oq=”);

HttpClient提供很多工具方法来简化创建和修改执行URI,例如:

List qparams = new ArrayList();

qparams.add(new BasicNameValuePair(“q”, “httpclient”));

qparams.add(new BasicNameValuePair(“btnG”, “Google Search”));

qparams.add(new BasicNameValuePair(“aq”, “f”));

qparams.add(new BasicNameValuePair(“oq”, null));

URI uri = URIUtils.createURI(“htt p”, “ww w.googl e.c om”, -1, “/search”,

URLEncodedUtils.format(qparams, “UTF-8”), null);

HttpGet httpget = new HttpGet(uri);

三、处理头部消息

一个HTTP消息可以包含一系列头部描述消息的属性。例如:内容长度、内容类型等。

HttpClient提供方法检索、添加、删除、枚举头部信息。

HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1,HttpStatus.SC_OK, “OK”);

response.addHeader(“Set-Cookie”,”c1=a; path=/; domain=localhost”);

response.addHeader(“Set-Cookie”,”c2=b; path=/; domain=localhost”);

//获取header的方法1

Header h1 = response.getFirstHeader(“Set-Cookie”);

Header h2 = response.getLastHeader(“Set-Cookie”);

//获取header的方法2

HeaderIterator it = response.headerIterator(“Set-Cookie”);

while (it.hasNext()) {

System.out.println(it.next());

}

//获取header的方法3

Header[] headers = response.getAllHeaders();

for (Header header : headers) {

System.out.println(header.getName() + ” | ” + header.getValue());

}

四、HTTP实体

HTTP规范定义了两种包装实体的方法:POST和PUT。响应通常附上内容实体。

HttpClient根据其内容出自何处区分三种类型的实体:

streamed(流式):内容从流中获得,或者在运行中产生。流式实体是不可重复生成的。

self-contained(自我包装式):内容在内存中或通过独立的连接或其它实体中获得。自我包装式的实体是可以重复生成和读取的。经常用于封装HTTP请求实体。(像ByteArrayEntity或StringEntity)。

wrapping(包裹式):内容从另外一个实体中获得。

如果从一个HTTP响应中获取流式内容,这个区别对于连接管理很重要。

如果是应用程序创建并用于发送的请求实体,这个类型区别就没那么重要了。

五、使用HTTP实体

使用实体常用的一些API,得到内容类型、内容长度、内容编码、内容转换成String或ByteArray、设置分块(HTTP1.1)。但是,EntityUtils的使用是不鼓励的,除非实体响应来自一个可信赖的HTTP服务器和已知的有限长度。

myEntity.getContentType()

myEntity.getContentLength()

EntityUtils.getContentCharSet(myEntity)

EntityUtils.toString(myEntity)

EntityUtils.toByteArray(myEntity)

myEntity.setChunked(true);

六、得到实体内容

HttpGet httpget = new HttpGet(“htt p:/ /localhost/”);

HttpResponse response = httpclient.execute(httpget);

HttpEntity entity = response.getEntity();

if (entity != null) {

long len = entity.getContentLength();

if (len != -1 && len < 2048) {

System.out.println(EntityUtils.toString(entity));

} else {

InputStream instream = entity.getContent();

int byteOne = instream.read();

int byteTwo = instream.read();

//终止请求,该连接将不被重用,保证底层的资源被正确释放

httpget.abort();

}

}

七、生成实体内容

HttpClient提供了常见的数据容器,比如字符串,字节数组,输入流和文件:StringEntity,ByteArrayEntity,InputStreamEntity和FileEntity。

File file = new File(“somefile.txt”);

FileEntity entity = new FileEntity(file, “text/plain; charset=\”UTF-8\”“);

HttpPost httppost = new HttpPost(“htt p:/ /localhost/action.do”);

httppost.setEntity(entity);

八、HTML表单(UrlEncodedFormEntity)

许多应用程序需要频繁模拟提交一个HTML表单的过程,比如,为了来记录一个Web应用程序或提交输出数据。HttpClient提供了特殊的实体类UrlEncodedFormEntity来这个满足过程。

List<NameValuePair> formparams = new ArrayList<NameValuePair>();
formparams.add(new BasicNameValuePair("param1", "value1"));
formparams.add(new BasicNameValuePair("param2", "value2"));
//生成的实体内容:param1=value1¶m2=value2
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, "UTF-8");
HttpPost httppost = new HttpPost("htt p:/ /localhost/handler.do");
httppost.setEntity(entity);


九、响应处理器(ResponseHandler)

ResponseHandler能够保证在任何情况下都会将底层的HTTP连接释放回连接管理器。用户不必担心HttpClient连接占用系统资源,可以把注意力集中在处理HTTP响应内容。

HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet("htt p:/ /localhost/");
ResponseHandler<byte[]> handler = new ResponseHandler<byte[]>() {
public byte[] handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
HttpEntity entity = response.getEntity();
if (entity != null) {
return EntityUtils.toByteArray(entity);
} else {
return null;
}
}
};
byte[] response = httpclient.execute(httpget, handler);
HttpClient还提供了很多高级的特性,如:连接管理、状态管理、认证管理、客户服务等。这里只介绍了一些基础的用法,有时间好好研究一下。


测试代码:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;

public class HttpClientTest {

private static String link = "http://www.baidu.com";

public static void main(String[] args) {
long a = System.currentTimeMillis();
useHttpURlConnection();
long b = System.currentTimeMillis();
System.out.println("use httpurlconnection: "+(b-a));
long c = System.currentTimeMillis();
useHttpClient();
long d = System.currentTimeMillis();
System.out.println("use httpclient: "+(d-c));
}

public static void useHttpURlConnection(){
HttpURLConnection conn = null;
URL url = null;
String result = "";
try {
url = new java.net.URL(link);
conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(10000);
conn.connect();

InputStream urlStream = conn.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(urlStream));
String s = "";
while ((s = reader.readLine()) != null) {
result += s;
}
System.out.println(result);
reader.close();
urlStream.close();
conn.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch(Exception e){
e.printStackTrace();
}
}

public static void useHttpClient(){
HttpClient client = new HttpClient();
GetMethod method = new GetMethod(link);
method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
new DefaultHttpMethodRetryHandler(3, false));
try {
int statusCode = client.executeMethod(method);

if (statusCode != HttpStatus.SC_OK) {
System.err.println("Method failed: " + method.getStatusLine());
}
byte[] responseBody = method.getResponseBody();
System.out.println(new String(responseBody));
} catch (HttpException e) {
System.err.println("Fatal protocol violation: " + e.getMessage());
e.printStackTrace();
} catch (IOException e) {
System.err.println("Fatal transport error: " + e.getMessage());
e.printStackTrace();
} finally {
method.releaseConnection();
}
}
}


相关文章:

HttpURLConnection和HTTPClient的比较,以及使用规则

http://blog.csdn.net/hguang_zjh/article/details/33743249
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: