gpt4 book ai didi

c - 如何从 http 响应中拆分 binary/post-data/html?

转载 作者:太空宇宙 更新时间:2023-11-03 23:55:07 25 4
gpt4 key购买 nike

我正在编写一个简单的函数,用于从 HTTP 响应中拆分二进制/后数据/html。 HTTP header 由 \r\n\r\n 终止,其余为消息。

我是这样写的:

 #define MAX_BUFFER_SIZE 256
//...
int size = 0;
int buf_size = MAX_BUFFER_SIZE;
char * headers = malloc(MAX_BUFFER_SIZE);
char * newbuf;

while(httpresponse[size]) {
if(httpresponse[size] == '\r' &&
httpresponse[size + 1] == '\n' &&
httpresponse[size + 2] == '\r' &&
httpresponse[size + 3] == '\n') {
break;
}

headers[size] = httpresponse[size];

if(size >= buf_size) {
buf_size += MAX_BUFFER_SIZE;
newbuf = realloc(headers, buf_size);

if(NULL == newbuf) exit(1);
headers = newbuf;

}

size ++;
}

printf("%s\n", headers);

httpresponse 变量,具有如下值:

HTTP/1.1 200 OK
Date: Fri, 23 Mar 2012 15:28:17 GMT
Expires: Sat, 23 Mar 2013 15:28:17 GMT
Cache-Control: public, max-age=31536000
Last-Modified: Thu, 14 Apr 2011 15:46:35 GMT
Content-Type: image/jpeg
Content-Length: 12745
X-XSS-Protection: 1; mode=block
Connection: close

���������I1��} �g������'�B�f�p���ohd]sft�����J�������1����瘿ٱ����$3�G�8��4=�E�i����ܼG����H��nbi�"�1��b[Ǘl��++���OPt�W��>�����i�]t�QT�N/,Q�Qz������0�` N7���M��f��S�Š�x9k��$*

//more binary...

但是上面的C程序,打印如下文字:

HTTP/1.1 200 OK
Date: Fri, 23 Mar 2012 17:12:09 GMT
Expires: Sat, 23 Mar 2013 17:12:09 GMT
Last-Modified: Thu, 14 Apr 2011 15:46:35 GMT
Content-Type: image/jpeg
Content-Length: 12745
X-XSS-Protection: 1; mode=block
Cache-Control: public, max-age=31536000
Age: 3746
�2�/���ms���|ނ����LQr2K3�v��J.�,�z��^Oy����s(ct���X`iA����I����U�{

代替:

    HTTP/1.1 200 OK
Date: Fri, 23 Mar 2012 15:28:17 GMT
Expires: Sat, 23 Mar 2013 15:28:17 GMT
Cache-Control: public, max-age=31536000
Last-Modified: Thu, 14 Apr 2011 15:46:35 GMT
Content-Type: image/jpeg
Content-Length: 12745
X-XSS-Protection: 1; mode=block
Connection: close

如何解决这个问题?提前致谢。

最佳答案

我使用以下代码:

const QByteArray& data = socket->readAll();
int index = data.indexOf("\r\n\r\n");
QString sHeader;
QString sBody;
if (index < 0)
sHeader = QString::fromUtf8(data);
else
{
sHeader = QString::fromUtf8(data.left(index));
sBody = QString::fromUtf8(data.mid(index + 4));
}

QIssHttpRequestHeader requestHeader(sHeader); // QIssHttpRequestHeader is a copy of QHttpRequestHeader from Qt4

if (requestHeader.method() != "GET")
{
send501Error(socket);
return;
}

QUrl url(requestHeader.path());
...

关于c - 如何从 http 响应中拆分 binary/post-data/html?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9844504/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com