我创建了自己的 HTTP 类,该类利用 QNAM 并提供发送 HTTP 请求的方法。它使用 QEventLoop 进行同步,使用 QTimer 进行超时。
我的解决方案面临一些问题。在某些 Symbian 平台上,我的 QTimer 信号超时太快(例如,在 1 秒后超时为 30 秒)。如果我的 HTTP Post 播放负载很大或者我通过 GET 下载文件(请求需要一些时间才能完成),通常会发生这种情况。我想指出,相同的代码在某些设备(S60 第 3 版)上运行良好,但另一方面,某些设备(第 5 版)几乎总是出现此错误。
这是一个代码片段:
MyHttp::MyHttp(QObject *parent) : QObject(parent)
{
m_Timer.setSingleShot(true);
connect(&m_Manager, SIGNAL(finished(QNetworkReply*)), SLOT(OnFinished(QNetworkReply*)));
connect(&m_Timer, SIGNAL(timeout()), SLOT(OnTimeout()));
}
void MyHttp::Post(const QString &data)
{
m_RetCode = 0;
QNetworkRequest request(url);
m_Reply = m_Manager.post(request, data.toAscii()); // QPointer<QNetworkReply> m_Reply
m_Timer.start(30*1000);
m_EventLoop.exec(); // Synchronization point
}
void MyHttp::OnFinished(QNetworkReply * reply)
{
// Handle response / Timeout / Errors
reply->deleteLater(); // Use deleteLater() as adviced in the documentation
StopWaiting();
}
void MyHttp::StopWaiting()
{
m_Timer.stop();
m_EventLoop.exit();
}
void MyHttp::OnTimeout()
{
m_RetCode = TIMEOUT; // #define TIMEOUT 50000
if(m_Reply.isNull() == false)
{
// Abort reply
m_Reply->abort();
}
}
我个人认为以下之一可能会导致问题:
- 重新进入本地事件循环会弄乱信号
- 我多次使用相同的 QNAM(在同一会话期间有多个请求)。这是必需的,因为如果我销毁 QNAM,我的会话将在服务器端关闭。
有没有人能够看到一些可能导致这种行为的错误?
平台:Symbian S60 3rd/5th edition
工具:诺基亚 Qt SDK