我正在尝试发送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会在第一个queryItem中自动添加?
标记。
答案 0 :(得分:2)
还有什么值得的,因为没有给出答案:
不要使用转换为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());