Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
249 views
in Technique[技术] by (71.8m points)

c++ - HTTP Headers and Data response, how to decode % in data?

I am producing a HTTP response from an Qt 5.15.2 application, the response is typically something like:

HTTP/1.1 200 OK
Server: XMLMPAM
Access-Control-Allow-Origin: *
Date: Sun, 24 Jan 2021 07:56:45 GMT
Content-Length: 65
Connection: Closed
Content-Type: text/html; charset=utf-8

<!DOCTYPE html><html><body>{%22msgType%22:%22hb%22}</body></html>

Here is the code that produces this:

static const QString scstrLineTerm("
");
QString strWrapper("<!DOCTYPE html><html><body>" + arybytData + "</body></html>")
       ,strDateTime(dtUTC.toString("ddd, dd MMM yyyy hh:mm:ss") + " GMT")
       ,strDataLength(QString::number(strWrapper.length()))
       ,strHTTP = QString("HTTP/1.1 200 OK%1"
                          "Server: XMLMPAM%2"
                          "Access-Control-Allow-Origin: *%3"
                          "Date: %4%5"
                          "Content-Length: %6%7"
                          "Connection: Closed%8"
                          "Content-Type: text/html; charset=utf-8%9"
                          "%10%11"
                          "%12").arg(scstrLineTerm
                                    ,scstrLineTerm
                                    ,scstrLineTerm
                                    ,strDateTime, scstrLineTerm
                                    ,strDataLength, scstrLineTerm
                                    ,scstrLineTerm
                                    ,scstrLineTerm
                                    ,scstrLineTerm, scstrLineTerm
                                    ,strWrapper);

This is the URL from the browser that goes with the response:

http://localhost:8123/?{%22msgType%22:%22hb%22}

The browser gets the response:

{%22msgType%22:%22hb%22}

What I want is a response without the escape codes so it looks like:

{"msgType":"hb"}
question from:https://stackoverflow.com/questions/65868264/http-headers-and-data-response-how-to-decode-in-data

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Solved:

QString strWrapper("<!DOCTYPE html><html><body>" + QUrl::fromPercentEncoding(arybytData) + "</body></html>")
       ,strDateTime(dtUTC.toString("ddd, dd MMM yyyy hh:mm:ss") + " GMT")
       ,strDataLength(QString::number(strWrapper.length()))
       ,strHTTP;
strHTTP = QString("HTTP/1.1 200 OK%1"
                  "Server: XMLMPAM%2"
                  "Access-Control-Allow-Origin: *%3"
                  "Date: %4%5"
                  "Content-Length: %6%7"
                  "Connection: Closed%8"
                  "Content-Type: text/html; charset=utf-8%9"
                  "%10%11"
                  "%12").arg(scstrLineTerm
                            ,scstrLineTerm
                            ,scstrLineTerm
                            ,strDateTime, scstrLineTerm
                            ,strDataLength, scstrLineTerm
                            ,scstrLineTerm
                            ,scstrLineTerm
                            ,scstrLineTerm, scstrLineTerm
                            ,strWrapper);

Response:

{"msgType":"hb"}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...