首先,我想在创建此类时触发初始化过程。(已知主机的 DNS 查找,并在指定数量的插槽上与主机进行预握手 - 一旦经过测试,就不应进行硬编码) - 在创建所有类时,正在读取配置等。
其次,我想根据特定主机的允许(和打开)插槽允许并行请求发送。
坦率地说,以下代码存在多个问题;
- 谷歌似乎在多个请求同时重定向到该主机似乎导致灾难性失败,当 x 数或请求触发完成信号时,该信号调用我的重定向检查并且第一个请求被重定向,我这样做似乎收到了第一个回复,并带有 x 条错误消息。
- 出于同样的原因,我似乎无法从 httpFinished 调用 deleteLater()
- 我找不到等待请求的槽号完成以启动队列中的下一个请求的方法
主要目标是使用单个网络类从多个 API 收集数据,这些请求可能会同时发生,也可能不会。还会像 POST 等一样发出另一个请求 -可能是 1 个类/API?
请注意,代码只是概念测试,仅用于测试目的,在修复后应进行清理,这根本不是我想要的工作方式。
星云.pro
QT += core network
QT -= gui
TARGET = nebula
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp \
network.cpp
HEADERS += \
network.h
网络.h
#ifndef NETWORK_H
#define NETWORK_H
#include <QObject>
#include <QtNetwork/QNetworkAccessManager>
#include <QtNetwork/QNetworkReply>
#include <QtNetwork/QHostInfo>
#include <QtNetwork/QSslConfiguration>
#include <QEventLoop>
class network : public QObject
{
Q_OBJECT
public:
explicit network(QObject *parent = 0);
~network();
void sendGet(const QUrl &url);
private:
QNetworkAccessManager networkAccessManager;
QHash<QString, QByteArray> usedSession;
QNetworkReply *reply;
QUrl url;
Q_INVOKABLE void init();
void replyFinished(QUrl &url, QNetworkReply *reply);
QList<QString> provideTcpHosts();
QList<QString> provideSslHosts();
QEventLoop eventLoop;
private slots:
void httpError();
void sslErrors(const QList<QSslError> &errors);
void httpFinished();
};
#endif // NETWORK_H
网络.cpp
#include "network.h"
/**
* @brief network::network
* initialazing pre-networking initialization once the eventpool is ready
* @param parent
*/
network::network(QObject *parent) : QObject(parent)
{
QMetaObject::invokeMethod(this, "init", Qt::QueuedConnection);
}
network::~network()
{
networkAccessManager.deleteLater();
}
/**
* @brief network::init
* dns chache warming for all hosts and pre-connecting to all hosts via 4 sockets
*/
void network::init()
{
QList<QString> tcpHosts = provideTcpHosts();
QList<QString> sslHosts = provideSslHosts();
// tcp hosts initialazation
for( int i=0; i<tcpHosts.count(); ++i )
{
// pre-dns lookup cache warming
QHostInfo::lookupHost(tcpHosts[i], 0, 0);
qDebug() << "pre-dns lookup cache warming for: " + tcpHosts[i];
// tcp pre-handshake with known hosts over 4 sockets with known http hosts
for(int a=0; a<4; ++a)
{
networkAccessManager.connectToHost(tcpHosts[i], 80);
qDebug() << "connecting " + QString::number(a+1) + "th socket for " + tcpHosts[i];
}
}
// tcp hosts initialazation
for( int i=0; i<sslHosts.count(); ++i )
{
// pre-dns lookup cache warming
QHostInfo::lookupHost(sslHosts[i], 0, 0);
qDebug() << "pre-dns lookup cache warming for: " + sslHosts[i];
// tcp pre-handshake with known hosts over 4 sockets with known http hosts
for(int a=0; a<4; ++a)
{
networkAccessManager.connectToHostEncrypted(sslHosts[i], 443);
qDebug() << "connecting " + QString::number(a+1) + "th socket for " + sslHosts[i];
}
}
}
/**
* @brief network::replyFinished
* storing previous ssl session tickets for re-use, and error handling for finished requests
* @param url
* @param reply
*/
void network::replyFinished(QUrl &url, QNetworkReply *reply)
{
if(!usedSession.contains((QString)url.toString()))
{
usedSession.insert((QString)url.toString(), (QByteArray)reply->sslConfiguration().sessionTicket());
qDebug() << "saved ssl session ticket for" + url.toString();
}
reply->deleteLater();
}
/**
* @brief network::sendGet
* sending a simple GET request to specified url
* @param url
*/
void network::sendGet(const QUrl &url)
{
connect(&networkAccessManager, SIGNAL(finished(QNetworkReply*)), &eventLoop, SLOT(quit()));
qDebug() << "Sending a GET request to" + (QString)url.toString();
QNetworkRequest request;
request.setUrl(url);
request.setRawHeader("User-Agent", "nebula");
// reusing an ssl session ticket if exists for url
if(usedSession.contains((QString)url.toString()))
{
QSslConfiguration qssl;
qssl.setSslOption(QSsl::SslOptionDisableSessionPersistence, false);
qssl.setSessionTicket(usedSession.value((QString)url.toString()));
request.setSslConfiguration(qssl);
qDebug() << "used ssl session ticket for" + url.toString();
}
reply = networkAccessManager.get(request);
connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(httpError()));
connect(reply, SIGNAL(sslErrors(QList<QSslError>)), this, SLOT(sslErrors(QList<QSslError>)));
connect(reply, SIGNAL(finished()), this, SLOT(httpFinished()));
}
/**
* @brief network::provideTcpHosts
* @return
*/
QList<QString> network::provideTcpHosts()
{
return QList<QString>()
<< (QString)"http://www.google.com"
<< (QString)"http://www.bing.com";
}
/**
* @brief network::provideSslHosts
* @return
*/
QList<QString> network::provideSslHosts()
{
return QList<QString>()
<< (QString)"https://www.ssllabs.com/ssltest/";
}
/*SLOTS*/
/**
* @brief network::slotTcpError
* @param tcpError
*/
void network::httpError()
{
qDebug() << "QNetworkRequest::HttpStatusCodeAttribute " << reply->attribute(QNetworkRequest::HttpStatusCodeAttribute ) << "received";
qDebug() << reply->errorString();
reply->deleteLater();
}
/**
* @brief network::slotSslErrors
* @param sslErrors
*/
void network::sslErrors(const QList<QSslError> &errors)
{
QString errorString;
foreach (const QSslError &error, errors) {
if (!errorString.isEmpty())
errorString += ", ";
errorString += error.errorString();
}
qDebug() << "ssl error recieved: " + errorString;
reply->deleteLater();
}
void network::httpFinished()
{
// possible redirect url
QVariant redirectionTarget = reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
if (!redirectionTarget.isNull()) {
this->sendGet(reply->url().resolved(reply->attribute(QNetworkRequest::RedirectionTargetAttribute).toUrl()));
} else {
qDebug() << "QNetworkRequest::HttpStatusCodeAttribute " << reply->attribute(QNetworkRequest::HttpStatusCodeAttribute ) << "received ";
qDebug() << reply->errorString();
qDebug() << reply->readAll();
}
}
主文件
#include <QCoreApplication>
#include <network.h>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
network networkAccessManager;
for(int i = 0; i < 50; ++i)
{
networkAccessManager.sendGet((QString)"http://www.google.com");
}
return a.exec();
}
请注意,我对高级 C++ 项目和 QT 还是很陌生,这个项目的一部分——但一旦松开,将用于我很棒的项目——是为了帮助我熟悉这些主题。
你能帮帮我吗,哦,光荣的陌生人?:)
问候; 独眼太空鱼