简体   繁体   English

Qt中HTTP GET响应的奇怪编码

[英]Strange Encoding on HTTP GET response in Qt

I am trying to simulate webpage activity through Qt (5.8) using QNetworkAccessManager. 我正在尝试使用QNetworkAccessManager通过Qt(5.8)模拟网页活动。 I have been using the FireBug plugin of Firefox to analyze each Get/Post request, copying the appropriate headers. 我一直在使用Firefox的FireBug插件来分析每个Get / Post请求,并复制相应的标头。 On my initial GET request, I get a QNetworkReply object. 在最初的GET请求中,我得到一个QNetworkReply对象。 I have inspected all of the headers, and it is the corrent response. 我检查了所有标题,这是正确的响应。 However, when I try and read the data, I get a strange format. 但是,当我尝试读取数据时,会得到一种奇怪的格式。 When viewing this packet in Wireshark, the HTML response shows up as it does in FireBug, something like: 在Wireshark中查看此数据包时,HTML响应会像在FireBug中一样显示,如下所示:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <meta http-equiv="Content-language" content="en" />...

However, when I am reading my response to a QByteArray, the debugger says that this variable is: 但是,当我读取对QByteArray的响应时,调试器说此变量为:

\037\213\010\000\000\000\000\000\000\003µW[SÛ8\024~Æ¿BÕÌNàÁv\234\033\t$Ù\013\224¶;)0\020¦»O...

and when saving this QByteArray to a text file, it looks like this: 将这个QByteArray保存到文本文件时,它看起来像这样:

‹      µW[SÛ8~Æ¿BÕÌNàÁvœ    $Ù”¶;)0¦»OÙ–mQÙr%™Àîìß#_s-Ýay òѹ|
ç*ɲ¦ïŽÏŽ–ž¿G‰N9:¿úmñéaÛu¿ô\÷xyŒþø¸ü¼@žÓEKI2Å4á®ûþ#î­õlÝÚuBâ¹5-
•Þ¦<S3œh¸îz½vÖ}GÈØõ&“I%
Ó'Y<Ã4Ãh³2:(    çÖÎ4¥š £Ä¦ß
v3ÃG"Ó4Óöò.§Õ×kz«]£ô    ‘ŠêÙÕòÄcä¾¨ÆØ,HÜVe <-Dos&©jñÚÞs¼¹$qJZ¬™°$
´-‘”Î0)t"d‹õ3‘šeè#¹áD?æD~'Yœè–

Can anyone tell me what I am doing wrong? 谁能告诉我我在做什么错? I feel like it is an encoding issue. 我觉得这是一个编码问题。 My code looks like: 我的代码如下:

void MainWindow::replyFinished(QNetworkReply *reply)
{
    if (reply->error() == QNetworkReply::NoError)
    {
        QByteArray getResponse = reply->readAll();
        QList<QNetworkReply::RawHeaderPair> headers = reply->rawHeaderPairs();
        output(QString(getResponse));
        QFile file("C:/Users/lrmlrm97/Desktop/Response.txt");
        if (file.open(QFile::WriteOnly))
        {
            for (int i = 0; i < headers.size(); i++)
            {
                file.write(headers.at(i).first + ": " + headers.at(i).second);
                file.write("\r\n");
            }
            file.write("\r\n\r\n");
            file.write(getResponse);
            file.write("\r\n\r\n");
            file.close();
        }
    }
}

Many thanks! 非常感谢!

Lucas 卢卡斯

I finally figured it out. 我终于弄明白了。 One of the headers I was sending was 我发送的标头之一是

request.setRawHeader("Accept-Encoding", "gzip, deflate");

which was causing the response to be compressed. 这导致响应被压缩。 Once I removed this line, everything worked perfectly. 一旦我删除了这一行,一切就可以正常工作了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2025 STACKOOM.COM