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

1k Views Asked by At

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

3

There are 3 best solutions below

1
On BEST ANSWER

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.

0
On

You can setPath() and setQuery() separatly. What come before the '?' is the path and after is the query arguments.

const QStringList path_part = path.split('?');
url.setPath(path_part.at(0));
if(path_part.size() > 1)
    url.setQuery(path_part.at(1));
0
On

The encoded URL seems valid, maybe a problem with your webserver?