2012-09-19 73 views
5

我試圖發送一個JSON查詢到一個Web服務,我繼續得到內部服務器錯誤作爲對查詢的響應。使用QNetworkRequest正確的HTTP POST格式

這裏是我想要送:

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

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()); 

回答

11

這不是寫你的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); 
+0

感謝您的快速響應! _italic_POST /api/1.7/webservice.asmx HTTP/1.1 _italic_行是多餘的,還是將如何插入到請求中? – DarwinIcesurfer

+2

這是多餘的。 */api/1.7/webservice.asmx *由'serviceURL'和* POST *與'QNetworkAccessManager :: post();'方法指定。 **您不必編寫「低級」HTTP請求。 Qt會爲你做這件事**。不要猶豫,閱讀一些關於這個教程,我認爲這對你非常有用。 –

+0

我的json字符串也不正確。我需要傳遞「方法」,「參數」和「ID」。我錯過了「參數」對象和「id」對象。 json的一個很好的總結[鏈接](http://en.wikipedia.org/wiki/JSON-RPC#Version_1.0) – DarwinIcesurfer