HttpURLConnection 与 Android HttpClient 对比

18 篇文章 0 订阅

HttpURLConnection 与 Android HttpClient 对比

9/15/2015 11:00:00 AM

   Android中Http请求方式的目前使用比较多的主要有HttpURLConnection 与 Android HttpClient 两种,接下来介绍两种方式的异同,最后以代码的形式重点介绍一下HttpURLConnection 的使用。

一、两种请求方式对比

  1. 是否带有gzip压缩
    HttpURLConnection默认带gzip压缩;
    Apache的HttpClient,默认不带gzip压缩;

  2. 两种方式默认的User-Agent不同

  3. 两种方式请求connection都是keep alive

二、两种请求方式选择

   在Froyo之前,HttpURLConnection 有个重大 Bug,调用 close() 函数会影响连接池,导致连接复用失效,所以在 Froyo 之前使用 HttpURLConnection 需要关闭 keepAlive。

   另外在 Gingerbread HttpURLConnection 默认开启了 gzip 压缩,提高了 HTTPS 的性能,Ice Cream Sandwich(4.0) HttpURLConnection 支持了请求结果缓存。再加上 HttpURLConnection 本身 API 相对简单,所以对 Android 来说,在 2.3 之后建议使用 HttpURLConnection,之前建议使用 Android HttpClient。

引用于作者Tim Bray给出的回答
Which client is best?
Apache HTTP client has fewer bugs on Eclair and Froyo. It is the best choice for these releases.
For Gingerbread and better, HttpURLConnection is the best choice. Its simple API and small size makes it great fit for Android. Transparent compression and response caching reduce network use, improve speed and save battery. New applications should use HttpURLConnection; it is where we will be spending our energy going forward.
备注:Gingerbread (Android 2.3 姜饼)
Froyo (Android 2.2 冻酸奶)
Éclair (Android 2.1)

三、HttpURLConnection 使用

URL url = new URL("http://localhost:8080/TestHttpURLConnectionPro.do");
HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();

// 设置是否向httpUrlConnection输出,因为这个是post请求,参数要放在
// http正文内,因此需要设为true, 默认情况下是false;
urlConn.setDoOutput(true);
// 设置是否从httpUrlConnection读入,默认情况下是true;
urlConn.setDoInput(true);
// Post 请求不能使用缓存
urlConn.setUseCaches(false);

// 设定传送的内容类型是可序列化的java对象
// (如果不设此项,在传送序列化对象时,当WEB服务默认的不是这种类型时可能抛java.io.EOFException)
urlConn.setRequestProperty("Content-type","application/x-java-serialized-object");

// 设定请求的方法为"POST",默认是GET
urlConn.setRequestMethod("POST");

// 连接,上面对urlConn的所有配置必须要在connect之前完成,
urlConn.connect();

// 此处getOutputStream会隐含的进行connect (即:如同调用上面的connect()方法,
// 所以在开发中不调用上述的connect()也可以)。
OutputStream outStrm = urlConn.getOutputStream();

// 现在通过输出流对象构建对象输出流对象,以实现输出可序列化的对象。
ObjectOutputStream oos = new ObjectOutputStream(outStrm);

// 向对象输出流写出数据,这些数据将存到内存缓冲区中
oos.writeObject(new String("我是测试数据"));

// 刷新对象输出流,将任何字节都写入潜在的流中(些处为ObjectOutputStream)
oos.flush();

// 关闭流对象。此时,不能再向对象输出流写入任何数据,先前写入的数据存在于内存缓冲区中,
// 再调用下边的getInputStream()函数时才把准备好的http请求正式发送到服务器
oos.close();

// 调用HttpURLConnection连接对象的getInputStream()函数,
// 将内存缓冲区中封装好的完整的HTTP请求电文发送到服务端。
InputStream inStrm = urlConn.getInputStream(); // <===注意,实际发送请求的代码段就在这里

/*
 * Post传参的方法
 */
OutputStream os = urlConn.getOutputStream();
String param = new String();
param = "CorpID=123&LoginName=qqq&name=" + URLEncoder.encode("汉字","GBK"); ;
os.write(param.getBytes());


/*
 *超时设置,防止 网络异常的情况下,可能会导致程序僵死而不继续往下执行
 */

//JDK 1.5以前的版本,只能通过设置这两个系统属性来控制网络超时:
//连接主机的超时时间(单位:毫秒)
System.setProperty("sun.net.client.defaultConnectTimeout", "30000"); 
//从主机读取数据的超时时间(单位:毫秒)
System.setProperty("sun.net.client.defaultReadTimeout", "30000"); 

//在JDK 1.5以后可以这样来设置超时时间
HttpURLConnection urlCon = (HttpURLConnection)url.openConnection();
urlCon.setConnectTimeout(30000);
urlCon.setReadTimeout(30000);

总结:

  • HttpURLConnection的connect()函数,实际上只是建立了一个与服务器的tcp连接,并没有实际发送http请求。
  • 无论是post还是get,http请求实际上直到HttpURLConnection的getInputStream()这个函数里面才正式发送出去。

  • 对HttpURLConnection对象的一切配置都必须要在connect()函数执行之前完成。而对outputStream的写操作,又必须要在inputStream的读操作之前。这些顺序实际上是由http请求的格式决定的。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值