Qt网络请求自动添加意外问号



我正在尝试发送http POST请求。以下是代码片段:

    const QUrl URL("https://httpbin.org/post");
    QNetworkRequest req(URL);
    req.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
    QUrlQuery urlQuery;
    urlQuery.addQueryItem ("username", username);
    urlQuery.addQueryItem ("password", password);
    QUrl params;
    params.setQuery (urlQuery);

    mNetReply = mNetMan->post(req, params.toEncoded());

这是响应输出:

QJsonObject({"args":{},"data":"","files":{},"form":{"?username":"xyz","password":"xyz"},"headers":{"Accept-Encoding":"gzip, deflate","Accept-Language":"en-US,*","Connection":"close","Content-Length":"28","Content-Type":"application/x-www-form-urlencoded","Host":"httpbin.org","User-Agent":"Mozilla/5.0"},"json":null,"origin":"*.*.*.*","url":"https://httpbin.org/post"})

我的问题是,qt会自动在第一个查询项中添加?标记。

对于它的价值,因为没有给出答案:

不要使用转换为QUrl

,使用QUrlQuery直接为我解决了问题。

urlQuery.toString(QUrl::FullyEncoded).toUtf8()

代替

params.toEncoded()

总片段已更新:

const QUrl URL("https://httpbin.org/post");
QNetworkRequest req(URL);
req.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
QUrlQuery urlQuery;
urlQuery.addQueryItem ("username", username);
urlQuery.addQueryItem ("password", password);
mNetReply = mNetMan->post(req, urlQuery.toString(QUrl::FullyEncoded).toUtf8());

相关内容

  • 没有找到相关文章

最新更新