这是HTTP请求发送方法。当目标网站响应时,将调用httpFinished()。
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()指向相应的管理器。
注意connect命令。 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
}
}
另一个更好的解决方案是设置reply属性并将其放入插槽。
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
}
}
}
关于c++ - 我怎么知道QNetWorkReply来自哪个代理?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59541246/