Fail to redirect by QnetworkAccessManager(but browser(chrome) could)



  • I would like to download the image locate at "image":http://2cat.komica.tw/~ericpony/5d.c/src/?5192778091347.png
    QNetworkAccessManager always fail, but the browser(chrome) could download the image without any problem.
    The browser would redirect(?) to another url "other url":http://2cat.komica.tw/~ericpony/5d.c/src/1347772915908.png

    I found some introduction about the redirect function of QNetworkAccessManager from here
    "introduction of redirect":https://www.developer.nokia.com/Community/Wiki/Handling_an_HTTP_redirect_with_QNetworkAccessManager
    But it can't work, below are my codes

    header
    @
    class networkTest : public QWidget
    {

    Q_OBJECT
    public:
    explicit networkTest(QWidget *parent = 0);

    private slots:
    void received_data();
    void request_data();

    private:
    QUrl redirect_url(QUrl const &possible_redirect_url, QUrl const &old_redirect_url) const;
    void request_data_impl(QString const &url);

    private:
    QNetworkAccessManager *manager_;
    QUrl redirected_url_;
    };

    #endif // NETWORKTEST_HPP

    @

    implement
    @
    /**********************************************************
    ****************** implementation ************************
    **********************************************************/

    void networkTest::received_data()
    {
    QNetworkReply reply = qobject_cast<QNetworkReply>(sender() ) ;

    QVariant possible_redirected_url_ = reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
    redirected_url_ = redirect_url(possible_redirected_url_.toUrl(), redirected_url_);
    
    if(!redirected_url_.isEmpty())
    {
        QString text = QString("Redirected to ").append(redirected_url_.toString());       
    
        request_data_impl(redirected_url_.toString() );
    }
    else
    {
        QString text = QString("Arrived to ").append(reply->url().toString());       
      
        //test it is a valid jpg or png or not
        QImage image;
        image.loadFromData(array);
    
        if(image.isNull())
            text_edit_->appendPlainText("image " + reply->url().toString() + " is null");
        else
        {
            QFile file&#40;"sample"&#41;;
            file.open(QIODevice::WriteOnly&#41;;
            file.write(array&#41;;
        }
    
        redirected_url_.clear(&#41;;
    }
    
    reply->deleteLater(&#41;;
    

    }

    void networkTest::request_data()
    {
    request_data_impl(line_edit_->text() );
    }

    void networkTest::request_data_impl(QString const &url)
    {
    QNetworkReply *reply = manager_->get(QNetworkRequest(url));
    connect(reply, SIGNAL(finished()), this, SLOT(received_data()) );
    }

    QUrl networkTest::redirect_url(QUrl const &possible_redirect_url, QUrl const &old_redirect_url) const
    {
    QUrl redirect_url;

    if(!possible_redirect_url.isEmpty() && possible_redirect_url != old_redirect_url)
    {
        redirect_url = possible_redirect_url;
    }
    
    return redirect_url;
    

    }
    @


  • Moderators

    Let's debug step-by-step (just like in http://qt-project.org/forums/viewthread/20012/ ) :)

    Is your redirected URL correct?



  • Is your redirected URL correct?

    @
    QVariant possible_redirected_url_ = reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
    @

    Always give me empty result, "image":http://2cat.komica.tw/~ericpony/5d.c/src/?5192778091347.png
    is the url I input, chrome would redirect me to "redirect":http://2cat.komica.tw/~ericpony/5d.c/src/1347772915908.png,
    but Qt would not, the possible_redirected_url_ is always empty.



  • Those image links are NSFW (and possibly illegal in some countries), please add that to your post.



  • I change the topic, and replace by another image
    Sorry to cause you the troubles, I will pay more attention next time



  • I change my code a little bit to print the result onto
    @
    QNetworkReply reply = qobject_cast<QNetworkReply>(sender() ) ;

    QByteArray array(reply->readAll() );
    text_edit_->appendPlainText(array);
    

    @
    The contents of the bytearray get from
    "fail to redirect":http://2cat.komica.tw/~ericpony/5d.c/src/?5192778091347.png

    @
    <html>
    <body>
    [removed]
    eval(function(p,a,c,k,e,d){e=function(c){return c};if(!''.replace(/^/,String)){while(c--){d[c]=k[c]||c}k=[function(e){return d[e]}];e=function(){return'\w+'};c=1};while(c--){if(k[c]){p=p.replace(new RegExp('\b'+e(c)+'\b','g'),k[c])}}return p}('4 3=")+,///*1)-1(0";4 6=9.11(0,12,15,14,7);4 5="";8(4 2=0;2<13;2++)5+=3.16(2);3=5+6.19(1);18.17(3);',10,20,'||i|a|var|bb|aa|103|for|String||fromCharCode|46||110|112|charCodeAt|replace|location|substr'.split('|'),0,{}))
    [removed]
    </body>
    </html>
    @

    The contents of the bytearray get from "redirect successful ":http://www.wikipedia.org/wiki/URL_redirection

    @
    <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
    <html><head>
    <title>301 Moved Permanently</title>
    </head><body>
    <h1>Moved Permanently</h1>
    <p>The document has moved <a href="http://en.wikipedia.org/wiki/URL_redirection">here</a>.</p>
    </body></html>
    @

    Looks like Qt can't redirect by the first contents of the QBtyeArray, or I choose a wrong
    direction to solve the problem?


  • Moderators

    [quote author="stereomatching" date="1347862664"]
    @
    QVariant possible_redirected_url_ = reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
    @

    Always give me empty result, "image":http://2cat.komica.tw/~ericpony/5d.c/src/?5192778091347.png
    is the url I input, chrome would redirect me to "redirect":http://2cat.komica.tw/~ericpony/5d.c/src/1347772915908.png,
    but Qt would not, the possible_redirected_url_ is always empty.[/quote]

    Then your issue is related to URL redirection, not about downloading JPEGs or PNGs. Also, since your links are not NSFW anymore, you can remove that label :)

    Just FYI, when you post help requests or bug reports, it's good to include only the code that's related to the issue, so that it's easier for others to read. In this case, you didn't have to include code for GUI, QFile, main.cpp, etc.

    Anyway, it looks like the network access library is not treating your request as a redirection. I'm not sure if this is caused by a bug in the library, or a special server setup, or something else.

    What do you get from reply->readAll()?


  • Moderators

    Ah, you beat me to it.

    [quote author="stereomatching" date="1347866961"]Looks like Qt can't redirect by the first contents of the QBtyeArray, or I choose a wrong direction to solve the problem?[/quote]

    Your failed-to-redirect data contains JavaScript. I'm not very sure, but I think QtNetwork can't handle Javascript commands. I know that QtWebKit handles JavaScript, but it's designed to display websites, not download files... Not sure what options you have, sorry

    [Edit]: Maybe you can use QtScript to evaluate everything inside the eval() function? (Line 4 of fail-to-redirect) This is just from a quick Google on "Qt JavaScript" -- I don't know if this is a good approach



  • Thanks for your help very much, I found another way to get the proper url without redirect
    The url write inside of the html, I could parse it and download the jpg directly.

    This problem is partially solved, maybe someday I need to deal with javascript(no good at it)


Log in to reply
 

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