下面是我是在VC6下获取收到的回应包,如何确定这个头的长度??
如果是Content-Length形式的可以直接获取后面的值,Transfer-Encoding的一个如何呢?
望高人指点一二。
谢谢了  HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
X-Powered-By: Servlet 2.4; JBoss-4.2.2.GA (build: SVNTag=JBoss_4_2_2_GA date=200710221139)/Tomcat-5.5
Transfer-Encoding: chunked
Date: Wed, 19 Aug 2009 01:25:19 GMT33
return=0;money=10.0;YDcount=0;LTcount=0;XLTcount=0;

解决方案 »

  1.   

    response header的最后会有"\r\n\r\n"标识,可以通过他来判断response header的结束
      

  2.   

    Transfer-Encoding为chunked时, 无法直接获数据的总长度, 只能从头读到尾.(Microsoft Platform SDK -- ISAPI示例中针对Transfer-Encoding==chunked的情况是这样处理的)
      

  3.   

    HTTP传输如果没有指定content-length,那么一般会使用chunked传输,
    如果是接收网页的话,还可以判断收到</html>时为结束.
      

  4.   


    那就只能根据"\r\n\r\n"来获取呢??
    那如何判断我数据的总长度已经接受完了呢?\r\n\r\n 后面还有数据
      

  5.   

    必须在request header中指定Range例如Range: bytes=0-\r\n,(表示从头接受到尾,也可以指定接受的范围,用于断点续传)
    response header中才会有Content-Length。可以根据后面的值判断文件的大小
      

  6.   

    楼上的朋友, response header不一定会包含Content-Length
    我觉得应该参照2楼的朋友处理方法,HTTP有时候是使用chunked传输方式的.
    楼主可以GOOGLE下关于chunked的接收方法.
      

  7.   

    谢谢提醒,确实如此,以前没遇到过这种情况。刚才查了一下rfc2616的说明,希望对LZ有所帮助
    3.6.1 Chunked Transfer Coding   The chunked encoding modifies the body of a message in order to
       transfer it as a series of chunks, each with its own size indicator,
       followed by an OPTIONAL trailer containing entity-header fields. This
       allows dynamically produced content to be transferred along with the
       information necessary for the recipient to verify that it has
       received the full message.       Chunked-Body   = *chunk
                            last-chunk
                            trailer
                            CRLF       chunk          = chunk-size [ chunk-extension ] CRLF
                            chunk-data CRLF
           chunk-size     = 1*HEX
           last-chunk     = 1*("0") [ chunk-extension ] CRLF       chunk-extension= *( ";" chunk-ext-name [ "=" chunk-ext-val ] )
           chunk-ext-name = token
           chunk-ext-val  = token | quoted-string
           chunk-data     = chunk-size(OCTET)
           trailer        = *(entity-header CRLF)   The chunk-size field is a string of hex digits indicating the size of
       the chunk. The chunked encoding is ended by any chunk whose size is
       zero, followed by the trailer, which is terminated by an empty line.   The trailer allows the sender to include additional HTTP header
       fields at the end of the message. The Trailer header field can be
       used to indicate which header fields are included in a trailer (see
       section 14.40).
    A server using chunked transfer-coding in a response MUST NOT use the
       trailer for any header fields unless at least one of the following is
       true:   a)the request included a TE header field that indicates "trailers" is
         acceptable in the transfer-coding of the  response, as described in
         section 14.39; or,   b)the server is the origin server for the response, the trailer
         fields consist entirely of optional metadata, and the recipient
         could use the message (in a manner acceptable to the origin server)
         without receiving this metadata.  In other words, the origin server
         is willing to accept the possibility that the trailer fields might
         be silently discarded along the path to the client.   This requirement prevents an interoperability failure when the
       message is being received by an HTTP/1.1 (or later) proxy and
       forwarded to an HTTP/1.0 recipient. It avoids a situation where
       compliance with the protocol would have necessitated a possibly
       infinite buffer on the proxy.   An example process for decoding a Chunked-Body is presented in
       appendix 19.4.6.   All HTTP/1.1 applications MUST be able to receive and decode the
       "chunked" transfer-coding, and MUST ignore chunk-extension extensions
       they do not understand.
      

  8.   

    Transfer-Encoding为chunked时, 无法直接获数据的总长度, 只能从头读到尾.那如何判断我已经全部获取到了呢 
      

  9.   

    分配一个固定大小的缓冲区, 循环读取,直至读取的字节数为0, 或报错
    const int READ_BUFFER_SIZE = 1<<16;
    DWORD cbRead;
    LPBYTE buffer=(LPBYTE)LocalAlloc(LPTR, READ_BUFFER_SIZE);
    BOOL rc;
    DWORD dwTotalBytes=0;
    do
    {
        cbRead=READ_BUFFER_SIZE;
        if(!ReadClient(buffer, &cbRead)) return FALSE;
        // do sth.
        dwTotalBytes+=cbRead;
    }while(cbRead>0);//do sth.return TRUE;