这是一个HTTP请求发送方法。当目标网站响应时,将调用httpDone((。
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?
}
我可以在循环中调用 HttpRequestGet((。
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();
}
调用HTTPClientBase::httpFinished(QNetWorkReply* reply)
时,我如何知道此回复来自哪个代理? }
根据 QNetworkReply 文档,您可以使用成员函数 QNetworkReply::request(( 获取相应的请求。
反正QNetworkRequest没有setProxy
的成员功能。
但是如果你正在为QNetworkAccessManager设置代理,你可以通过QNetworkReply::manager((有一个指向相应管理器的指针。
请注意连接命令。finished()
没有QNetworkReply*
参数,因此您的connect
命令也将失败,您必须HTTPClientBase::httpFinished(QNetWorkReply* reply)
更改为HTTPClientBase::httpFinished()
。
...
connect(reply, SIGNAL(finished()), this,
SLOT(httpFinished()));
}
void HTTPClientBase::httpFinished()
{
QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
if(reply){
QNetworkAccessManager * manager = reply->manager();
QNetworkProxy proxy = manager->proxy();
// now you have the proxy
}
}
如您所见,您必须使用 sender(( 来获取实际的信号发送器。 您需要为您拥有的每个代理创建不同的QNetworkAccessManager实例。如果您有代理池,请先创建 QNetworkAccessManager 实例,然后根据您的特定需求进行选择。
如果你不想为每个代理创建一个新的QNetworkAccessManager,你可以这样做:
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);
//new member variable: QHash<QNetworkReply*,QString> m_RequestToProxy;
m_RequestToProxy.insert(reply,proxy);
connect(reply, SIGNAL(finished()), this, SLOT(httpFinished()));
}
void HTTPClientBase::httpFinished()
{
QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
if(reply){
QString proxy = m_RequestToProxy.take(reply);
//check if proxy is valid
//and then do whatever you want
}
}
另一个更好的解决方案是设置回复属性并将其放入插槽中。
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);
reply->setProperty("_proxy_",proxy);
connect(reply, SIGNAL(finished()), this, SLOT(httpFinished()));
}
void HTTPClientBase::httpFinished()
{
QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
if(reply){
QVariant v_proxy = reply->property("_proxy_");
//check if proxy is valid
if(v_proxy.isValid()){
QString proxy = v_proxy.toString();
//and then do whatever you want
}
}
}