Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QtNetwork. How to send multiple simultaneous requests via multiple proxy servers?
Forum Updated to NodeBB v4.3 + New Features

QtNetwork. How to send multiple simultaneous requests via multiple proxy servers?

Scheduled Pinned Locked Moved General and Desktop
1 Posts 1 Posters 1.5k 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.
  • Y Offline
    Y Offline
    Yivo
    wrote on 27 Jul 2013, 01:12 last edited by
    #1

    Hello. First of all I want to apologize for my bad English. Okay...
    I have list of accounts in format login:password and list of proxies ip:port. My task is to authorize an account and verify some of its characteristics. It is simple to do if I have one connection.

    By it sounds good if I will be able to send, for example, five requests (POST) from proxies A, B, C, D, E at the same time. Than, if some of them replied I can send request (GET) from the same proxies. If some of them replies I can check content of reply. When all requests replied I select new proxies and repeat the process until all accounts are checked.

    Please, see the pictures below to understand better what I am talking about...
    !https://dl.dropboxusercontent.com/u/58160380/alg.PNG(algorithm)!

    I am using Qt 5.1 and now I have this...
    (few comments because the function names represent what they are doing)

    @/// Types
    class MyQNetworkAccessManager: public QNetworkAccessManager
    {
    Q_OBJECT

    public:
    MyQNetworkAccessManager():
    QNetworkAccessManager(nullptr),
    account( {"", ""} ) {}
    std::pair<QString, QString> account; // I need network manager to know what account it is checking
    };

    /// Variables...
    QVector<MyQNetworkAccessManager*> nam;
    QVector<QNetworkProxy> proxies;
    QVector<std::pair<QString, QString>> accounts;
    qint32 threads; //the size of nam is equal to this

    /// slots...
    void authorizationDone(QNetworkReply *reply);
    void openHomepageDone(QNetworkReply *reply);

    /// functions...
    void authorizationRequest(QNetworkAccessManager *manager);
    void openHomepageRequest(QNetworkAccessManager *manager);
    void process(); // main funtion which send initial requests

    // simply casts base object to derived
    MyQNetworkAccessManager* castToDerived(QNetworkAccessManager *base) const;

    /// implementation...

    MyQNetworkAccessManager* castToDerived(QNetworkAccessManager base) const
    {
    return static_cast<MyQNetworkAccessManager
    >(base);
    }

    void process()
    {
    foreach(MyQNetworkAccessManager *ptr, nam) {
    authorizationRequest( ptr );
    }
    }

    void authorizationRequest(QNetworkAccessManager *manager)
    {
    if ( accounts.isEmpty() || proxies.isEmpty() ) {
    return;
    }

    manager->clearAccessCache();
    

    // set next proxy
    manager->setProxy( proxies.back() );
    proxies.pop_back();

    // set next account
    castToDerived(manager)->account = accounts.back();
    accounts.pop_back();

    // send POST request
    manager->post(postUrl, postData);

    // when reply is finished it means that auth done
    connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(authorizationDone(QNetworkReply*)));
    }

    void openHomepageRequest(QNetworkAccessManager manager)
    {
    manager->get(getRequest);
    connect(manager, SIGNAL(finished(QNetworkReply
    )), this, SLOT(openHomepageDone(QNetworkReply*)));
    }

    void authorizationDone(QNetworkReply reply)
    {
    disconnect(reply->manager(), SIGNAL(finished(QNetworkReply
    )), this, SLOT(authorizationDone(QNetworkReply*)));

    if ( reply->error() == QNetworkReply::NoError ) {
           openHomepageRequest(reply->manager());
        }
    } else {
        authorizationRequest(reply->manager());
    }
    

    }

    void openHomepageDone(QNetworkReply reply)
    {
    disconnect(reply->manager(), SIGNAL(finished(QNetworkReply
    )), this, SLOT(openHomepageDone(QNetworkReply*)));

    if (reply->error() == QNetworkReply::NoError) {
    

    // extract some info from reply
    }
    authorizationRequest(reply->manager());
    }
    @

    This code works.
    !!! But I think it is bad idea to use vector of network managers because documentation says that application should have only one manages. One manager is enough.

    How can I rewrite this code or algorithm to become better? What to read or study?

    Thank you.

    1 Reply Last reply
    0

    1/1

    27 Jul 2013, 01:12

    • Login

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