gpt4 book ai didi

c++ - 我怎么知道 QNetWorkReply 来自哪个代理?

转载 作者:太空宇宙 更新时间:2023-11-03 17:25:09 25 4
gpt4 key购买 nike

是一种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() 获得指向相应管理器的指针.

注意连接命令。 finished() 没有 QNetworkReply* 参数,因此您的 connect 命令也会失败,您必须更改 HTTPClientBase::httpFinished(QNetWorkReply * 回复)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
}

}
}

关于c++ - 我怎么知道 QNetWorkReply 来自哪个代理?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59541246/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com