Correct format for HTTP POST using QNetworkRequest(使用 QNetworkRequest 的 HTTP POST 正确格式)
问题描述
我正在尝试向 Web 服务发送 JSON 查询,但我继续收到内部服务器错误作为对查询的响应.
I'm trying to send a JSON query to a web service and I continue to get internal server errors as a response to the query.
这是我要发送的内容:
POST /api/1.7/webservice.asmx HTTP/1.1
Host: www.superService.com
User-Agent: My app name v0.1
X-Custom-User-Agent: My app name v0.1
Content-Type: application/json
Content-Length:81
{"method":"AuthenticatePlain","loginName":"username@domain.com","password":"mypass"}
这应该发送到 https://www.superService.com/api/1.7/ssapi.asmx
在编写QNetworkRequest
时,用什么方法插入行
In preparing the QNetworkRequest
, what method is used to insert the line
POST/api/1.7/webservice.asmx HTTP/1.1?
QNetworkRequest
对象中是否包含完整的标头?
JSON 数据应该在 QNetworkRequest
对象中,还是作为 QNetworkAccessManager::post()
方法中的第二个参数添加到帖子中?
Is the complete header contained in the QNetworkRequest
object?
Should the JSON data be in the QNetworkRequest
object or is that added to the post as the second argument in the QNetworkAccessManager::post()
method?
这是我当前在 on_btnLogin_clicked()
槽中的代码:
Here is my current code in the on_btnLogin_clicked()
slot:
connect(m_qnam, SIGNAL(finished(QNetworkReply*)),
this, SLOT(handleNetworkData(QNetworkReply*)));
connect(m_qnam,SIGNAL(sslErrors(QNetworkReply*,QList<QSslError>)),
this, SLOT(handleSSLErrors(QNetworkReply*)));
QString baseString = "";
baseString.append(QString("POST /api/1.7/webservice.asmx HTTP/1.1
").toUtf8());
baseString.append(QString("www.superService.com
").toUtf8());
baseString.append(QString("User-Agent: My app name v0.1
").toUtf8());
baseString.append(QString("X-Custom-User-Agent: My app name v0.1
").toUtf8());
baseString.append(QString("Content-Type: application/json
").toUtf8());
QString jsonString = QString("{");
jsonString.append(""method":");
jsonString.append(""AuthenticatePlain"");
jsonString.append(","loginName":");
jsonString.append(""username@domain.com"");
jsonString.append(","password":");
jsonString.append(""mypass"");
jsonString.append("}");
QByteArray json = jsonString.toUtf8();
baseString.append(QString("Content-Length:").toUtf8());
baseString.append(QString::number(json.length()));
baseString.append("
").toUtf8();
baseString.append(QString("
").toUtf8());
baseString.append(json);
request = QNetworkRequest(QUrl("https://www.superService.com/api/1.7/ssapi.asmx"));
request.setRawHeader()
qDebug() << "Base String: "<< baseString;
m_qnam->post(request,baseString.toUtf8());
推荐答案
这不是编写 HTTP 请求的正确方法.下面这段代码更正确:
This is not the right way to write your HTTP request. The following piece of code is more correct :
connect(m_qnam, SIGNAL(finished(QNetworkReply*)), this, SLOT(handleNetworkData(QNetworkReply*)));
connect(m_qnam,SIGNAL(sslErrors(QNetworkReply*,QList<QSslError>)), this, SLOT(handleSSLErrors(QNetworkReply*)));
// Build your JSON string as usual
QByteArray jsonString = "{"method":"AuthenticatePlain","loginName":"username@domain.com","password":"mypass"}";
// For your "Content-Length" header
QByteArray postDataSize = QByteArray::number(jsonString.size());
// Time for building your request
QUrl serviceURL("https://www.superService.com/api/1.7/ssapi.asmx");
QNetworkRequest request(serviceURL);
// Add the headers specifying their names and their values with the following method : void QNetworkRequest::setRawHeader(const QByteArray & headerName, const QByteArray & headerValue);
request.setRawHeader("User-Agent", "My app name v0.1");
request.setRawHeader("X-Custom-User-Agent", "My app name v0.1");
request.setRawHeader("Content-Type", "application/json");
request.setRawHeader("Content-Length", postDataSize);
// Use QNetworkReply * QNetworkAccessManager::post(const QNetworkRequest & request, const QByteArray & data); to send your request. Qt will rearrange everything correctly.
QNetworkReply * reply = m_qnam->post(request, jsonString);
这篇关于使用 QNetworkRequest 的 HTTP POST 正确格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 QNetworkRequest 的 HTTP POST 正确格式
基础教程推荐
- 设计字符串本地化的最佳方法 2022-01-01
- 如何定义双括号/双迭代器运算符,类似于向量的向量? 2022-01-01
- 如何在 C++ 中处理或避免堆栈溢出 2022-01-01
- C++,'if' 表达式中的变量声明 2021-01-01
- 调用std::Package_TASK::Get_Future()时可能出现争用情况 2022-12-17
- C++ 程序在执行 std::string 分配时总是崩溃 2022-01-01
- 运算符重载的基本规则和习语是什么? 2022-10-31
- C++ 标准:取消引用 NULL 指针以获取引用? 2021-01-01
- 什么是T&&(双与号)在 C++11 中是什么意思? 2022-11-04
- 您如何将 CreateThread 用于属于类成员的函数? 2021-01-01