我怎么知道 QNetWorkReply 来自哪个代理?
How do I know a QNetWorkReply comes from which proxy?
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 document, you can get the corresponding request using the member function QNetworkReply::request().
无论如何,QNetworkRequest没有setProxy
.
的成员函数
但是如果您正在为 QNetworkAccessManager you can have a pointer to the corresponding manager by 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 instances for each proxy you have. If you have a proxy pool, create your 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
}
}
}
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 document, you can get the corresponding request using the member function QNetworkReply::request().
无论如何,QNetworkRequest没有setProxy
.
但是如果您正在为 QNetworkAccessManager you can have a pointer to the corresponding manager by 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 instances for each proxy you have. If you have a proxy pool, create your 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
}
}
}