服务器返回 HTTP 头和二进制文件;像这样的东西:
HTTP/1.1 200 OK
Date: Thu, 28 Jun 2012 22:11:14 GMT
Server: Apache/2.2.3 (Red Hat)
Set-Cookie: JSESSIONID=blabla; Path=/
Pragma: no-cache
Cache-Control: must-revalidate, no-store
Expires: Thu, 01 Jan 1970 00:00:00 GMT
Content-disposition: inline; filename="foo.pdf"
Content-Length: 6231119
Connection: close
Content-Type: application/pdf
%PDF-1.6
%âãÏÓ
5989 0 obj
<</Linearized 1/L 6231119/O 5992/E 371504/N 1498/T 6111290/H [ 55176 6052]>>
endobj
xref
5989 2744
0000000016 00000 n
0000061228 00000 n
0000061378 00000 n
我只想复制二进制文件。但是如何知道标题部分何时结束?我尝试检查该行是否包含 a\r\n\r\n
但看起来该标准不适用于服务器响应,仅适用于客户端。这给出了:
Content-disposition: inline; filename="foo.pdf"
Content-Length: 6231119
Connection: close
Content-Type: application/pdf
%PDF-1.6
%âãÏÓ
5989 0 obj
<</Linearized 1/L 6231119/O 5992/E 371504/N 1498/T 6111290/H [ 55176 6052]>>
endobj
xref
5989 2744
0000000016 00000 n
这是C代码:
while((readed = recv(sock, buffer, 128, 0)) > 0) {
if(isnheader == 0 && strstr(buffer, "\r\n\r\n") != NULL)
isnheader = 1;
if(isnheader)
fwrite(buffer, 1, readed, fp);
}
更新:
continue
我在 if 语句中添加了一个控件:
if(isnheader == 0 && strstr(buffer, "\r\n\r\n") != NULL) {
isnheader = 1;
continue;
}
嗯,它按预期工作。但正如@Alnitak 所提到的,这并不安全。