我正在尝试将JSON查询发送到Web服务,并且继续收到内部服务器错误作为对该查询的响应。

这是我要发送的内容:

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":"[email protected]","password":"mypass"}

这应该发送到https://www.superService.com/api/1.7/ssapi.asmx

在准备QNetworkRequest时,使用什么方法插入行

POST/api/1.7/webservice.asmx HTTP/1.1 吗?

完整的 header 包含在QNetworkRequest对象中吗?
JSON数据应该在QNetworkRequest对象中还是应作为QNetworkAccessManager::post()方法中的第二个参数添加到帖子中?

这是我在on_btnLogin_clicked()插槽中的当前代码:
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\r\n").toUtf8());
    baseString.append(QString("www.superService.com\r\n").toUtf8());
    baseString.append(QString("User-Agent: My app name v0.1\r\n").toUtf8());
    baseString.append(QString("X-Custom-User-Agent: My app name v0.1\r\n").toUtf8());
    baseString.append(QString("Content-Type: application/json\r\n").toUtf8());

    QString jsonString = QString("{");
    jsonString.append("\"method\":");
    jsonString.append("\"AuthenticatePlain\"");
    jsonString.append(",\"loginName\":");
    jsonString.append("\"[email protected]\"");
    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("\r\n").toUtf8();
    baseString.append(QString("\r\n").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请求的正确方法。以下代码更正确:

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\":\"[email protected]\",\"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);

08-04 17:15