简体   繁体   中英

QT: QUrl with no percent encoding

QT: I was wondering if there is a way to create a QUrl or another URL class object from string without encoding the final URL. For example, here is a snippet of my code:

QString GetJsonStringFromURL(QString url) //url == "192.168.0.111/controller?POSITION|03|100"
{
    QEventLoop eventLoop;
    QNetworkAccessManager mgr;
    QObject::connect(&mgr, SIGNAL(finished(QNetworkReply*)), &eventLoop, SLOT(quit()));  

    QNetworkRequest req( QUrl::fromUserInput(url) ) ; 

    QNetworkReply *reply = mgr.get(req);
    eventLoop.exec(); //wait till reply finished
    QString strReply = reply->readAll();
    return strReply;
}

This code accesses a local area network controller requesting it's json with a get method, while passing a parameter in the url. The url i'm passing gets percent encoded to:

"192.168.0.111/controller?POSITION%7C03%7C100"

which i am trying to avoid. The server i'm trying to access is a custom piece of hardware with firmware written in C which doesn't incorporate percent decoding. I would like to avoid doing maintenance on the server side. I have tried going through QUrl class reference but none of the available methods provided the wanted result.

This is not possible with QNetworkRequest since each request requires a QUrl object that does store the destination and is internally converted into an encoded string.

setRawHeader does not work for you, since it can set all HTTP headers except from the URL, that is part of the GET request.

Confirm raw HTTP header format curl -v http://stackoverflow.com/questions/

GET /questions/ HTTP/1.1
User-Agent: curl/7.37.1
Host: stackoverflow.com
Accept: */*

Thus you need an entirely raw socket connection (which I have no experience with).

Qurl has a decode function. Look at QUrl::decode() otherwise you can prevent encoding when passing it in with QUrl::fromPercentEncoding(url.toEncoded()) but you might lose some string integrity if you have complex query strings.

I have not personally used this but I though I would post as you mentioned you couldn't find anything in the class reference.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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