Qt web requests

  • Hello :)
    I decided it's time to learn networking in qt, and am now trying to log into a forum/online game/whatever using qt. I barely know anything about networking in general, so I guess I will have a hard time. From what I've understand, I need to use QNetworkCookieJar and QNetworkRequest for this. My question is, how do I retrieve the request information from a webpage('s login button), in order to write in into the qt code ?
    Take as example this forum, do I need to go to the login page, and find some specific data in the page source, like the action that is triggerd when pressing the login buttom, and then use it in the qt program to make the request ? Or how is it done ?

  • When you say you want to log into a forum, what is the end result you want your app to do with that log in? The way a site like this works is that the login page transmits your login information via an HTTP POST request to a specific script on the server. The server validates that information and sets a cookie on your machine with the information it needs to maintain that login. How exactly it works will vary from site to site, but that's the general process.

  • Thanks Chris. I actually want to log into an online browser game and get some data from the account, which is displayed in the home page after logging in. What I'm asking is how can I get the information from the website needed to make the post request in qt. I researched a bit more and think I have to take a look into chromes Network tab from Developer Options(the inspect element window)

  • Lifetime Qt Champion


    Did you check whether that site provides an API for that kind of stuff ?

  • Yep, there's no thing like that.

  • You can look at the Network tab in Chrome, but it's actually probably easier to simply examine the source code of the login page and look for the URL that's being submitted to. Then look at the html form elements that are being used to create the POST request and you'll have your data. You are then going to be reduced to writing a screen-scraper, unfortunately, since without an API all you can do is read whatever the server sends you and hope they don't change the page too often.

  • I managed to log in by creating a request with data obtained from network tab in chrome.
    This is the flow:
    Firstly I go to the login page using get function. From the obtained reply's header I get a sessionId(from Set-Cookie) . With that sessionId and other data obtained from studying the log in request in chrome, I make a post request and log in to the game. The operation is successful, because providing an incorrect user name/password does not return a specific Location header. Now again, with data from the previous post reply's cookie header I make a get request(just as chrome shows in the network tab) to download the html page. But here comes a problem, when I'm doing qDebug()<<QString(reply->readAll()); , instead of getting the html source code I get the following line : "\u001F?\b" . The respone header of the get request looks like the one in chrome, I don't know what goes wrong.

    Here is my code. I know it looks very bad but I want to keep everything in one single function until I get it to work.:

       QNetworkAccessManager* qnam=new QNetworkAccessManager(this);
       QNetworkReply* reply;
       QEventLoop loop;
       QString lognum(reply->readAll());
       lognum=lognum.mid(lognum.indexOf("						<input type=\"hidden\" name=\"login\" value=\"")+47,10);    // i need this value from the log in page source code for the query params, see below
       QByteArray sid(reply->rawHeader("Set-Cookie"));       //sessionid
    qDebug()<<"-------------------------Response Header of log in page reply---------------------------------------";
           for(QNetworkReply::RawHeaderPair x: reply->rawHeaderPairs()){
                qDebug()<<QString(x.first)<<"       "<<QString(x.second);
        QUrlQuery params;
        QNetworkRequest req(QUrl("http://tx3.travian.de/dorf1.php"));
        req.setRawHeader("Accept-Encoding","gzip, deflate");
        req.setRawHeader("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36");
        params.addQueryItem("name", "username");
        params.addQueryItem("password", "passw");
        params.addQueryItem("s1", "Einloggen");
        params.addQueryItem("login", lognum);       //here
        params.addQueryItem("lowRes", "0");
        reply=qnam->post(req, params.toString(QUrl::FullyEncoded).toUtf8());
    qDebug()<<"-------------------------Response Header of logged in page POST reply ---------------------------------------";
        for(QNetworkReply::RawHeaderPair x: reply->rawHeaderPairs()){
             qDebug()<<QString(x.first)<<"       "<<QString(x.second);
       QNetworkRequest wtr(QUrl("http://tx3.travian.de/dorf1.php"));
       QString getCookie=QString(reply->rawHeader("Set-Cookie"));
       getCookie.remove("path=/; httponly");
       getCookie.push_front(sid+"; ");
        wtr.setRawHeader("Accept-Encoding","gzip, deflate, sdch");
        wtr.setRawHeader("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36");
    qDebug()<<"-------------------------Response Header of logged in page GET reply ---------------------------------------";
            for(QNetworkReply::RawHeaderPair x: reply->rawHeaderPairs()){
                 qDebug()<<QString(x.first)<<"       "<<QString(x.second);
            qDebug()<<QString(reply->readAll());   // here , strange result

  • The status code of the final get request is 200, and the error in NoError. There's no redirection also... I 'm still trying to find out what's wrong with the reply I get.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.