-1

次の文字列があります。

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でこれを行う方法は?

4

2 に答える 2

6

これは、最初の 2 つの連続した改行を単純に見つけることで簡単に実行できます。これはヘッダーを終了し、そのポイントまでのすべてを別のバッファーにコピーするためです。

// Find the end of the header, which is terminated
// by two line breaks
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 here
    
        // Don't forget to release the allocated memory
        free(buffer);
    }
}
于 2011-12-06T12:46:32.223 に答える
1

strstr関数を使用できます。

ただし、HTTP クライアント ( libcurlなど) および HTTP サーバー ( onionなど) 側には優れた C ライブラリが存在することに注意してください。そして、そのHTTPは細部が複雑です。

于 2011-12-06T12:46:10.787 に答える