It's a HTTP request sending method. When the goal website responses, httpFinished() will be called.
void HTTPClientBase:: HttpRequestGet()
{
network_manager.setProxy(proxy);
QNetworkRequest network_request;
network_request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
network_request.setUrl(URL);
reply = network_manager.get(network_request);
connect(reply, SIGNAL(finished(QNetWorkReply*)), this, SLOT(httpFinished(QNetWorkReply*)));
}
void HTTPClientBase::httpFinished(QNetWorkReply* reply)
{
// How do I know this reply comes from which proxy?
}
I can call HttpRequestGet() in a loop.
static HTTPClientBase myClient;
for(int i=0; i<20; i++)
{
myClient.setUrl("https:\\www.google.com");
myClient.setProxy("123.123.123.123:1234"); // The proxy changes every time in this loop.
myClient.HttpRequestGet();
}
When HTTPClientBase::httpFinished(QNetWorkReply* reply) is called, How do I know this reply comes from which proxy?
}
As per the QNetworkReply document, you can get the corresponding request using the member function QNetworkReply::request().
Anyway, QNetworkRequest has not member function of
setProxy.But if you are setting proxy for QNetworkAccessManager you can have a pointer to the corresponding manager by QNetworkReply::manager().
Notice the connect command.
finished()has noQNetworkReply*argument so yourconnectcommand will also fail and you have to changeHTTPClientBase::httpFinished(QNetWorkReply* reply)toHTTPClientBase::httpFinished().As you see, you have to use sender() to obtain the actual signal sender. You need to create different QNetworkAccessManager instances for each proxy you have. If you have a proxy pool, create your QNetworkAccessManager instances first and choose them according to your specific needs.
If you don't want to create a new QNetworkAccessManager for each proxy, you can do something like this:
And another better solution is to set a property of reply and get it in slot.