开发者

Post request via QNetworkAccessManager

I have a problem while working with an object of QNetworkAccessManager class. I want to send a POST request to a web server. My code is

 QNetworkAccessManager *manager; 
    manager = new QNetworkAccessManager (); 
    QNetworkRequest req; 
    req.setUrl(QUrl("http://example.com")); 
    //Configure the parameters for the post request: 
    QByteArray postData; 
    postData.append("Login=log_name&"); 
    postData.append("Pas开发者_如何转开发sword=some_pass"); 
    //Now create a QCookieJar: 
    manager->setCookieJar(new QNetworkCookieJar(manager)); 
    //Define the Request-url: 
    connect (manager, SIGNAL(finished(QNetworkReply *)), this, SLOT(replyFinish (QNetworkReply  *))); 
    //Send the request: 
    manager->post(req, postData); 

The code of the used SLOT is:

void MainWindow::replyFinish(QNetworkReply *reply) 
   { 
    QString answer = QString::fromUtf8(reply->readAll()); 
     qDebug () << answer; 
   } 

The problem is that the answer is an empty string,but i believe it should be some html-code that describes acceptance or rejection of the authorization.

Thank you for your help.


Most sites use a HTTP redirection after a login request (or after filling a form). And you need to test for error in the same slot.

You can handle redirections (and errors) with something similar to that code.

Since QNetworkAccessManager already create its own base QNetworkCookieJar object, you don't need to instantiate one if you didn't write a derived class or if you don't share a cookie jar with an other manager. But you'll need to use the same manager (or the same cookie jar) for subsequent request to be able to reuse the cookies set by the server during the login.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜