解析HttpProtocolParams的setUseExpectContinu函数(Expect 100-continue)

在分析这个问题之前我们需要了解一下什么是“Expect 100Continue”和它到底是怎么去影响我们的http请求的,我们可以先看一下官方的解释是:

Activates 'Expect: 100-Continue' handshake for the entity enclosing methods. The 'Expect: 100-Continue' handshake allows a client that is sending a request message with a request body to determine if the origin server is willing to accept the request (based on the request headers) before the client sends the request body.

The use of the 'Expect: 100-continue' handshake can result in noticeable performance improvement for entity enclosing requests (such asPOST andPUT) that require the target server's authentication.

'Expect: 100-continue' handshake should be used with caution, as it may cause problems with HTTP servers and proxies that do not supportHTTP/1.1 protocol.

Expect 100Continue其实是http协议1.1中的一个header属性, 就是说如果设置了Expect 100Continue 那么就意味着客户端在向服务器发送数据的时候,需要先向服务器发起一个请求看服务器是否愿意接受客户端将要发送的数据(我们这里可以认为是http body,往往是较大的数据块)。

因为Expect 100 comtinue会导致客户端在向服务器发送数据是进行两次请求,这样对通信的性能方面将会受到一定的影响,但这种情况在做验证或者给予curl的服务器是很常见的,这也是该属性的主要应用场合。

介于以上原因,我们不能滥用该属性,并且在不支持http 1.1的协议的服务器也会产生一定的问题。


在回到setUseExpectContinu函数,我们看一下它的源代码:

public static void  [More ...] setUseExpectContinue(final HttpParams params, boolean b) {
        if (params == null) {
            throw new IllegalArgumentException("HTTP parameters may not be null");
        }     params.setBooleanParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, b);
}

而 public static final String USE_EXPECT_CONTINUE = "http.protocol.expect-continue";

也正是我们上面说的Expect 100Continue,如果我们在用HttpClient做为网络底层接口时候注意设置 HttpProtocolParams.setUseExpectContinue(params,false); 原因我在上面已经说得很清楚了。

关于setUseExpectContinue可见我的另外一篇文章《Android网络开发中出现NoHttpResponseException 或者ClientProtocolException解决方法 》 ;







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值