Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt WebKit
  4. QNetworkReply and 302 Found code
Forum Updated to NodeBB v4.3 + New Features

QNetworkReply and 302 Found code

Scheduled Pinned Locked Moved Qt WebKit
9 Posts 5 Posters 11.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    campo85
    wrote on last edited by
    #1

    Hi all,
    I am developing an application that uses the Qt Webkit. I have this code :

    @
    void SignalHandler::ddlogin()
    {
    Manager = new QNetworkAccessManager();
    QObject::connect(Manager, SIGNAL(finished(QNetworkReply *)), this, SLOT(finished_login(QNetworkReply *)));
    QNetworkRequest request;
    request.setUrl(QUrl("http://ddunlimited.net/ucp.php?mode=login"));
    request.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("application/x-www-form-urlencoded"));
    request.setRawHeader("User-Agent", "Firefox/3.0.10");
    QUrl params;
    params.addQueryItem("username", *(Config->get_user_forum()) );
    params.addQueryItem("password", *(Config->get_pass_forum()));
    params.addQueryItem("redirect","index.php");
    params.addQueryItem("login", "Login");
    params.encodedQuery();
    QNetworkReply *test = Manager->post(request,params.encodedQuery());
    }

    void SignalHandler::finished_login(QNetworkReply *reply)
    {
    QObject::disconnect(Manager, SIGNAL(finished(QNetworkReply *)),this,SLOT(finished_login(QNetworkReply *)));
    QByteArray *temp = new QByteArray(reply->readAll());
    QFile file("temp.html");
    file.open(QIODevice::WriteOnly);
    file.write(*temp);
    file.close();
    }
    @

    This code should make the login in forums based on phpbb3. It works for several forums, but it doesn't work for a forum. The problem is that the reply->readAll() return an empty QByteArray. The same code works for others forum, and it returns the html page after the login.

    I tried to debug the problem with a network analyzer and I saw that the server responds with the http response status code 302. It seems a sort of redirect, but I'm not sure. How can I handle this ? Is there a workaround ? Sorry for my english and sorry for my lack of experience.

    Kind regards,
    Stefano.

    1 Reply Last reply
    0
    • T Offline
      T Offline
      tomma
      wrote on last edited by
      #2

      Follow redirects. From "QNetworkReply doc:":http://qt-project.org/doc/qt-5.0/qtnetwork/qnetworkreply.html Note: When the HTTP protocol returns a redirect no error will be reported. You can check if there is a redirect with the QNetworkRequest::RedirectionTargetAttribute attribute.

      1 Reply Last reply
      0
      • raven-worxR Offline
        raven-worxR Offline
        raven-worx
        Moderators
        wrote on last edited by
        #3

        example can be found "here":http://developer.nokia.com/Community/Wiki/Handling_an_HTTP_redirect_with_QNetworkAccessManager.

        --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
        If you have a question please use the forum so others can benefit from the solution in the future

        1 Reply Last reply
        0
        • C Offline
          C Offline
          campo85
          wrote on last edited by
          #4

          [quote author="raven-worx" date="1376389368"]example can be found "here":http://developer.nokia.com/Community/Wiki/Handling_an_HTTP_redirect_with_QNetworkAccessManager.[/quote]

          I've already see the example but it's a little bit different from my case. To make a successfull login I have to use post request, after that I usually obtain a page and inside the html code I extract a parameter called "ssid". This "ssid" is used in the next request like an authentication token. I'm not sure that the example can help me bacause it uses a get request and without this "ssid" I'll never login.

          I don't know if I was clear. Now I'm trying to join and adapt the example code, but I'm not sure that it will work. Thank for the help and thank for any reply.

          Kind regards,
          Stefano.

          1 Reply Last reply
          0
          • raven-worxR Offline
            raven-worxR Offline
            raven-worx
            Moderators
            wrote on last edited by
            #5

            the example just shows how to handle the redirect. It's independent if POST or GET.
            If you detect a redirect (example) you do the same request like you did initially but only to the new redirected url.

            --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
            If you have a question please use the forum so others can benefit from the solution in the future

            A 1 Reply Last reply
            0
            • raven-worxR raven-worx

              the example just shows how to handle the redirect. It's independent if POST or GET.
              If you detect a redirect (example) you do the same request like you did initially but only to the new redirected url.

              A Offline
              A Offline
              Ahmed000001
              wrote on last edited by
              #6

              @raven-worx the provided link is invalid

              jsulmJ 1 Reply Last reply
              0
              • A Ahmed000001

                @raven-worx the provided link is invalid

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @ahmed000001 Because it is 6 years old and Qt does not belong to Nokia anymore.

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                A 1 Reply Last reply
                3
                • jsulmJ jsulm

                  @ahmed000001 Because it is 6 years old and Qt does not belong to Nokia anymore.

                  A Offline
                  A Offline
                  Ahmed000001
                  wrote on last edited by
                  #8

                  @jsulm i know but can you provide a corresponding link

                  jsulmJ 1 Reply Last reply
                  0
                  • A Ahmed000001

                    @jsulm i know but can you provide a corresponding link

                    jsulmJ Offline
                    jsulmJ Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @ahmed000001 See https://doc.qt.io/archives/qt-5.10/qnetworkreply.html#redirected
                    Set QNetworkRequest::FollowRedirectsAttribute in your request, then you will get the above signal with new URL if your request is redirected.

                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    1

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved