c++qtencodingqnetworkrequest

QNetworkRequest URL containing '?' - not requesting properly because of QUrl encoding


I have the next code to make a request:

void HTTPClient::post(QString connectionString, QHttpMultiPart* _multiPart, bool returnProgress) {
    QUrl url;
    if (ssl)
        url.setScheme("https");
    else
        url.setScheme("http");
    url.setHost(host);
    url.setPort(port);
    url.setPath(connectionString);
    url.setUrl(url.toEncoded());
    QNetworkRequest request(url);
    request.setRawHeader("User-Agent", QCoreApplication::applicationName().toLatin1());

    /*...irrelevant code...*/
}

The requested url should be

https://somewebpage.domain:443/REST/login.php?method=login_md5

but the QNetworkRequest requests this one despite I set the url as encoded (debugging url.toEncoded() prints the '?' correctly):

https://somewebpage.domain:443/REST/login.php%3Fmethod=login_md5

This results in a 404 not found page. I have tried setting the url with url.toString() and just url, but the '?' keeps messing up. What can I do to request the link properly?

I have tried building the QUrl in the constructor like this:

QUrl url("https://"+host+port+connectionString);

But results in the next string:

https://somewebpage.xn--domain-efa/REST/login.php?method=login_md5


Solution

  • You can try QUrl::fromEncoded

    url.setUrl( QUrl::fromPercentEncoding(url.toEncoded()));

    Parses input and returns the corresponding QUrl. input is assumed to be in encoded form, containing only ASCII characters.

    to correctly handle characters.