收到HTTP/1.1 100 Continue 后该怎么做,在windows7下用libcurl以post发送100K的数据返回的。

jackxiang 2016-1-20 14:36 | |
背景:试着把libcurl进行post到服务器102k分片时,发现返回:100 continue怎么办?
PHP修改:
CURLOPT_HTTPHEADER => array("Content-Type: application/binary","Expect:")
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
               // Disable Expect: header (lighttpd does not support it)

libcurl修改为:



      使用HTTP/1.1协议的curl,发送一个请求,在post数据量超过1K的时候,接口会返回:

  HTTP/1.1 100 Continue

  HTTP/1.1 200 OK
  Date: Sat, 07 Dec 2013 10:09:11 GMT
  Server: Apache/2.2.24 (Unix) PHP/5.3.25
  X-Powered-By: PHP/5.3.25
  Content-Length: 43
  Content-Type: text/html

  从表面上看,100 Continue表明请求一次没发送完,需要继续发送;搜索后看到现在新浪微博PHP架构师的一片博文,介绍了100的信息;详见在使用curl做POST的时候, 当要POST的数据大于1024字节的时候, curl并不会直接就发起POST请求, 而是会分为俩步,;

  流程如下:
  1. 发送一个请求, 包含一个Expect:100-continue, 询问Server使用愿意接受数据
  2. 接收到Server返回的100-continue应答以后, 才把数据POST给Server

  使用libcurl的时候会碰到这样的问题;
  在w3c的官网介绍看到如下一段话:

  8.2.3 Use of the 100 (Continue) Status
  The purpose of the 100 (Continue) status (see section 10.1.1) is to allow 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. In some cases, it might either be inappropriate or highly inefficient for the client to send the body if the server will reject the message without looking at the body.
  Requirements for HTTP/1.1 clients:
  – If a client will wait for a 100 (Continue) response before
  sending the request body, it MUST send an Expect request-header
  field (section 14.20) with the “100-continue” expectation.
  – A client MUST NOT send an Expect request-header field (section
  14.20) with the “100-continue” expectation if it does not intend
  to send a request body.

  简单翻译一下:
  使用100(不中断,继续)状态码的目的是为了在客户端发出请求体之前,让服务器根据客户端发出的请求信息(根据请求的头信息)来决定是否愿意接受来自客户端的包含了请求内容的请求;在某些情况下,在有些情况下,如果服务器拒绝查看消息主体,这时客户端发送消息主体是不合适的或会降低效率

  对HTTP/1.1客户端的要求:
  -如果客户端在发送请求体之前,想等待服务器返回100状态码,那么客户端必须要发送一个Expect请求头信息,即:”100-continue”请求头信息;

  -如果一个客户端不打算发送请求体的时候,一定不要使用“100-continue”发送Expect的请求头信息;

来自:http://zhidao.baidu.com/question/1495713785713961379.html?fr=iks&word=HTTP%2F1.1+100+Continue&ie=gbk
参考:http://www.laruence.com/2011/01/20/1840.html

作者:jackxiang@向东博客 专注WEB应用 构架之美 --- 构架之美,在于尽态极妍 | 应用之美,在于药到病除
地址:http://jackxiang.com/post/8451/
版权所有。转载时必须以链接形式注明作者和原始出处及本声明!


最后编辑: jackxiang 编辑于2016-1-20 15:00
评论列表
发表评论

昵称

网址

电邮

打开HTML 打开UBB 打开表情 隐藏 记住我 [登入] [注册]