我有以下字符串:
HTTP/1.1 200 OK
Date: Tue, 06 Dec 2011 11:53:22 GMT
Server: Apache/2.2.14 (Ubuntu)
X-Powered-By: PHP/5.3.2-1ubuntu4.9
Vary: Accept-Encoding
Content-Encoding: gzip
Content-Length: 48
Content-Type: text/html
��(�ͱ���I�O����H�����ч��
�4�@��AQ������t
我想提取到"\r\n\r\n"
,丢弃二进制数据。即:
HTTP/1.1 200 OK
Date: Tue, 06 Dec 2011 11:53:22 GMT
Server: Apache/2.2.14 (Ubuntu)
X-Powered-By: PHP/5.3.2-1ubuntu4.9
Vary: Accept-Encoding
Content-Encoding: gzip
Content-Length: 48
Content-Type: text/html
如何在C中执行此操作?
答案 0 :(得分:6)
这可以通过简单地找到前两个连续的换行符来轻松完成,因为这会终止标头,然后将所有内容复制到一个单独的缓冲区:
// find the end of the header, which is terminated
// by two linebreaks
char* end = strstr(header, "\r\n\r\n");
char* buffer = 0;
if(end) {
// allocate memory to hold the entire header
buffer = malloc((end - header) + 1);
if(buffer) {
// copy header to buffer
memcpy(buffer, header, end - header);
// null terminate the buffer
buffer[end - header] = 0;
// do something with the buffer
// don't forget to release the allocated memory
free(buffer);
}
}
答案 1 :(得分:1)