1
0
mirror of https://gitee.com/drabel/LibQQt.git synced 2025-01-04 10:18:44 +08:00
LibQQt/src/network/qqtwebaccessmanager.cpp

465 lines
12 KiB
C++
Raw Normal View History

2017-11-22 19:22:53 +08:00
#include "qqtwebaccessmanager.h"
#include "qqtcore.h"
2017-11-23 12:33:32 +08:00
QQtWebAccessManager::QQtWebAccessManager ( QObject* parent ) : QNetworkAccessManager ( parent )
2017-11-22 19:22:53 +08:00
{
2017-11-23 12:33:32 +08:00
manager = new QQtWebAccessSessionManager ( this );
2017-11-22 19:22:53 +08:00
2017-11-23 12:33:32 +08:00
connect ( this, SIGNAL ( finished ( QNetworkReply* ) ),
this, SLOT ( finished ( QNetworkReply* ) ) );
2017-11-22 19:22:53 +08:00
2017-11-23 12:33:32 +08:00
connect ( this, SIGNAL ( authenticationRequired ( QNetworkReply*, QAuthenticator* ) ),
this, SLOT ( authenticationRequired ( QNetworkReply*, QAuthenticator* ) ) );
2017-11-22 19:22:53 +08:00
2017-11-23 12:33:32 +08:00
connect ( this, SIGNAL ( proxyAuthenticationRequired ( QNetworkProxy, QAuthenticator* ) ),
this, SLOT ( proxyAuthenticationRequired ( QNetworkProxy, QAuthenticator* ) ) );
2017-11-22 19:22:53 +08:00
2017-11-23 12:33:32 +08:00
connect ( this, SIGNAL ( sslErrors ( QNetworkReply*, QList<QSslError> ) ),
this, SLOT ( sslErrors ( QNetworkReply*, QList<QSslError> ) ) );
2017-11-22 19:22:53 +08:00
2017-11-23 12:33:32 +08:00
connect ( this, SIGNAL ( networkAccessibleChanged ( QNetworkAccessManager::NetworkAccessibility ) ),
this, SLOT ( networkAccessibleChanged ( QNetworkAccessManager::NetworkAccessibility ) ) );
2017-11-22 19:22:53 +08:00
2017-11-23 12:33:32 +08:00
connect ( this, SIGNAL ( networkSessionConnected() ),
this, SLOT ( networkSessionConnected() ) );
2017-11-22 19:22:53 +08:00
}
2017-11-23 12:33:32 +08:00
void QQtWebAccessManager::sendGetRequest ( QQtWebAccessSession* session )
2017-11-22 19:22:53 +08:00
{
2017-11-23 12:33:32 +08:00
QTimer* timer = session->getTimer();
connect ( timer, SIGNAL ( timeout() ),
this, SLOT ( localReplyTimeOut() ) ); //超时信号
2017-11-22 19:22:53 +08:00
QNetworkRequest netRequest;
2017-11-23 12:33:32 +08:00
netRequest.setHeader ( QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded" );
netRequest.setUrl ( QUrl ( session->getWebAccessUrl() ) ); //地址信息
2017-11-22 19:22:53 +08:00
2017-11-23 12:33:32 +08:00
if ( session->getWebAccessUrl().toLower().startsWith ( "https" ) ) //https请求需ssl支持(下载openssl拷贝libeay32.dll和ssleay32.dll文件至Qt bin目录或程序运行目录)
2017-11-22 19:22:53 +08:00
{
QSslConfiguration sslConfig;
2017-11-23 12:33:32 +08:00
sslConfig.setPeerVerifyMode ( QSslSocket::VerifyNone );
sslConfig.setProtocol ( QSsl::TlsV1_1 );
netRequest.setSslConfiguration ( sslConfig );
2017-11-22 19:22:53 +08:00
}
2017-11-23 12:33:32 +08:00
QNetworkReply* reply = NULL;
reply = get ( netRequest );
session->setWebAccessReply ( reply ); //发起get请求
2017-11-22 19:22:53 +08:00
/*下面关联信号和槽*/
/*下载完成后开始一阵一阵堆取数据*/
2017-11-23 12:33:32 +08:00
connect ( reply, SIGNAL ( readyRead() ),
this, SLOT ( localReadyRead() ) );
2017-11-22 19:22:53 +08:00
/*有可用数据时 */
2017-11-23 12:33:32 +08:00
connect ( reply, SIGNAL ( downloadProgress ( qint64, qint64 ) ),
this, SLOT ( localUpdateDownloadProgress ( qint64, qint64 ) ) );
connect ( reply, SIGNAL ( uploadProgress ( qint64, qint64 ) ),
this, SLOT ( localUpdateUploadProgress ( qint64, qint64 ) ) );
2017-11-22 19:22:53 +08:00
//ignore
// connect ( s0->m_pNetworkReply, SIGNAL ( finished() ),
// this, SLOT ( finished(QNetworkReply*); ) ); //请求完成信号
2017-11-23 12:33:32 +08:00
timer->start();
}
2017-11-23 19:12:48 +08:00
QQtWebAccessSession* QQtWebAccessManager::sendGetRequest ( QString strUrl )
2017-11-23 12:33:32 +08:00
{
QQtWebAccessSession* session = manager->newWebAccessSession();
session->setWebAccessUrl ( strUrl );
sendGetRequest ( session );
}
QQtWebAccessSession* QQtWebAccessManager::sendGetRequests ( QStringList& strUrl )
{
}
QQtWebAccessSession* QQtWebAccessManager::sendGetRequest ( QNetworkRequest& netRequest )
{
2017-11-22 19:22:53 +08:00
}
2017-11-23 12:33:32 +08:00
QQtWebAccessSession* QQtWebAccessManager::sendGetRequests ( QList<QNetworkRequest*>& netRequests )
{
}
2017-11-23 19:12:48 +08:00
QQtWebAccessSession* QQtWebAccessManager::sendPostRequest ( QString strUrl )
2017-11-22 19:22:53 +08:00
{
//post
// QString strBody; //http body部分可封装参数信息
// QByteArray contentByteArray = strBody.toLatin1();//转成二进制
// m_pNetworkReply = m_pNetworkManager->post(netRequest,contentByteArray);//发起post请求
}
2017-11-23 12:33:32 +08:00
void QQtWebAccessManager::finished ( QNetworkReply* reply )
2017-11-22 19:22:53 +08:00
{
2017-11-23 12:33:32 +08:00
//pline() << reply;
2017-11-22 19:22:53 +08:00
//clear resource
2017-11-23 12:33:32 +08:00
QQtWebAccessSession* s0 = manager->getSessionByReply ( reply );
if ( !s0 )
return;
//pline() << s0->getWebAccessReply() << s0->getTimer();
emit replyFinished ( s0 );
s0->getTimer()->stop();
s0->getWebAccessReply()->deleteLater();
manager->removeWebAccessSession ( s0 );
}
void QQtWebAccessManager::authenticationRequired ( QNetworkReply* r, QAuthenticator* a )
{
//pline() << r << a;
}
void QQtWebAccessManager::proxyAuthenticationRequired ( QNetworkProxy p, QAuthenticator* a )
{
//pline() << p.hostName() << a;
}
void QQtWebAccessManager::sslErrors ( QNetworkReply* r, QList<QSslError> e )
{
//pline() << r << e.size();
}
void QQtWebAccessManager::networkAccessibleChanged ( QNetworkAccessManager::NetworkAccessibility a )
{
//pline() << a;
}
void QQtWebAccessManager::networkSessionConnected()
{
//pline();
}
void QQtWebAccessManager::localReplyTimeOut()
{
QTimer* timer = ( QTimer* ) sender();
pline() << timer;
QQtWebAccessSession* s0 = manager->getSessionByTimer ( timer );
if ( !s0 )
return;
emit replyTimeOut ( s0 ); //请求失败
s0->getWebAccessReply()->deleteLater();
manager->removeWebAccessSession ( s0 );
}
void QQtWebAccessManager::localReadyRead()
{
QNetworkReply* obj = ( QNetworkReply* ) sender();
//pline() << obj;
QQtWebAccessSession* session = manager->getSessionByReply ( obj );
emit readyRead ( session );
}
void QQtWebAccessManager::localUpdateUploadProgress ( qint64 bytesSent, qint64 bytesTotal )
{
QNetworkReply* obj = ( QNetworkReply* ) sender();
//pline() << obj;
QQtWebAccessSession* session = manager->getSessionByReply ( obj );
emit updateUploadProgress ( session, bytesSent, bytesTotal );
}
void QQtWebAccessManager::localUpdateDownloadProgress ( qint64 bytesReceived, qint64 bytesTotal )
{
QNetworkReply* obj = ( QNetworkReply* ) sender();
//pline() << obj;
QQtWebAccessSession* session = manager->getSessionByReply ( obj );
emit updateDownloadProgress ( session, bytesReceived, bytesTotal );
}
QQtWebAccessSession* QQtWebAccessSessionManager::newWebAccessSession()
{
QUuid uuid = QUuid::createUuid();
QQtWebAccessSession* session = new QQtWebAccessSession ( this );
session->setWebAccessSessionName ( uuid.toString() );
m_listWebAccessSession.push_back ( session );
return session;
}
QNetworkReply* QQtWebAccessSessionManager::getReplyHandlerByUrl ( QString& strUrl )
{
2017-11-22 19:22:53 +08:00
QQtWebAccessSession* s0 = NULL;
2017-11-23 12:33:32 +08:00
QListIterator<QQtWebAccessSession*> itor ( m_listWebAccessSession );
2017-11-22 19:22:53 +08:00
2017-11-23 12:33:32 +08:00
while ( itor.hasNext() )
2017-11-22 19:22:53 +08:00
{
QQtWebAccessSession* s1 = itor.next();
2017-11-23 12:33:32 +08:00
if ( s1->getWebAccessUrl() == strUrl )
2017-11-22 19:22:53 +08:00
{
s0 = s1;
break;
}
}
2017-11-23 12:33:32 +08:00
if ( !s0 )
return NULL;
return s0->getWebAccessReply();
}
QString QQtWebAccessSessionManager::getSessionNameByUrl ( QString& strUrl )
{
QQtWebAccessSession* s0 = NULL;
QListIterator<QQtWebAccessSession*> itor ( m_listWebAccessSession );
while ( itor.hasNext() )
{
QQtWebAccessSession* s1 = itor.next();
if ( s1->getWebAccessUrl() == strUrl )
{
s0 = s1;
break;
}
}
if ( !s0 )
return QString();
return s0->getWebAccessSessionName();
}
QQtWebAccessSession* QQtWebAccessSessionManager::getSessionByUrl ( QString& strUrl )
{
QQtWebAccessSession* s0 = NULL;
QListIterator<QQtWebAccessSession*> itor ( m_listWebAccessSession );
while ( itor.hasNext() )
{
QQtWebAccessSession* s1 = itor.next();
if ( s1->getWebAccessUrl() == strUrl )
{
s0 = s1;
break;
}
}
return s0;
}
QString QQtWebAccessSessionManager::getRequestUrlByReply ( QNetworkReply* reply )
{
QQtWebAccessSession* s0 = NULL;
QListIterator<QQtWebAccessSession*> itor ( m_listWebAccessSession );
while ( itor.hasNext() )
{
QQtWebAccessSession* s1 = itor.next();
if ( s1->getWebAccessReply() == reply )
{
s0 = s1;
break;
}
}
2017-11-22 19:22:53 +08:00
2017-11-23 12:33:32 +08:00
if ( !s0 )
return QString();
2017-11-22 19:22:53 +08:00
2017-11-23 12:33:32 +08:00
return s0->getWebAccessUrl();
2017-11-22 19:22:53 +08:00
}
2017-11-23 12:33:32 +08:00
QString QQtWebAccessSessionManager::getSessionNameByReply ( QNetworkReply* reply )
2017-11-22 19:22:53 +08:00
{
2017-11-23 12:33:32 +08:00
QQtWebAccessSession* s0 = NULL;
QListIterator<QQtWebAccessSession*> itor ( m_listWebAccessSession );
while ( itor.hasNext() )
{
QQtWebAccessSession* s1 = itor.next();
if ( s1->getWebAccessReply() == reply )
{
s0 = s1;
break;
}
}
if ( !s0 )
return QString();
return s0->getWebAccessSessionName();
2017-11-22 19:22:53 +08:00
}
2017-11-23 12:33:32 +08:00
QQtWebAccessSession* QQtWebAccessSessionManager::getSessionByReply ( QNetworkReply* reply )
2017-11-22 19:22:53 +08:00
{
2017-11-23 12:33:32 +08:00
QQtWebAccessSession* s0 = NULL;
QListIterator<QQtWebAccessSession*> itor ( m_listWebAccessSession );
while ( itor.hasNext() )
{
QQtWebAccessSession* s1 = itor.next();
if ( s1->getWebAccessReply() == reply )
{
s0 = s1;
break;
}
}
return s0;
2017-11-22 19:22:53 +08:00
}
2017-11-23 12:33:32 +08:00
QNetworkReply* QQtWebAccessSessionManager::getReplyHandlerBySessionName ( QString& strSessionName )
2017-11-22 19:22:53 +08:00
{
2017-11-23 12:33:32 +08:00
QQtWebAccessSession* s0 = NULL;
QListIterator<QQtWebAccessSession*> itor ( m_listWebAccessSession );
while ( itor.hasNext() )
{
QQtWebAccessSession* s1 = itor.next();
if ( s1->getWebAccessSessionName() == strSessionName )
{
s0 = s1;
break;
}
}
if ( !s0 )
return NULL;
return s0->getWebAccessReply();
2017-11-22 19:22:53 +08:00
}
2017-11-23 12:33:32 +08:00
QString QQtWebAccessSessionManager::getRequestUrlBySessionName ( QString& strSessionName )
2017-11-22 19:22:53 +08:00
{
2017-11-23 12:33:32 +08:00
QQtWebAccessSession* s0 = NULL;
QListIterator<QQtWebAccessSession*> itor ( m_listWebAccessSession );
while ( itor.hasNext() )
{
QQtWebAccessSession* s1 = itor.next();
if ( s1->getWebAccessSessionName() == strSessionName )
{
s0 = s1;
break;
}
}
if ( !s0 )
return QString();
return s0->getWebAccessUrl();
2017-11-22 19:22:53 +08:00
}
2017-11-23 12:33:32 +08:00
QQtWebAccessSession* QQtWebAccessSessionManager::getSessionBySessionName ( QString& strSessionName )
2017-11-22 19:22:53 +08:00
{
2017-11-23 12:33:32 +08:00
QQtWebAccessSession* s0 = NULL;
QListIterator<QQtWebAccessSession*> itor ( m_listWebAccessSession );
while ( itor.hasNext() )
{
QQtWebAccessSession* s1 = itor.next();
if ( s1->getWebAccessSessionName() == strSessionName )
{
s0 = s1;
break;
}
}
return s0;
2017-11-22 19:22:53 +08:00
}
2017-11-23 12:33:32 +08:00
QNetworkReply* QQtWebAccessSessionManager::getReplyHandlerByTimer ( QTimer* timer )
2017-11-22 19:22:53 +08:00
{
2017-11-23 12:33:32 +08:00
QQtWebAccessSession* s0 = NULL;
QListIterator<QQtWebAccessSession*> itor ( m_listWebAccessSession );
while ( itor.hasNext() )
{
QQtWebAccessSession* s1 = itor.next();
2017-11-22 19:22:53 +08:00
2017-11-23 12:33:32 +08:00
if ( s1->getTimer() == timer )
{
s0 = s1;
break;
}
}
if ( !s0 )
return NULL;
return s0->getWebAccessReply();
}
QString QQtWebAccessSessionManager::getRequestUrlByTimer ( QTimer* timer )
{
2017-11-22 19:22:53 +08:00
QQtWebAccessSession* s0 = NULL;
2017-11-23 12:33:32 +08:00
QListIterator<QQtWebAccessSession*> itor ( m_listWebAccessSession );
2017-11-22 19:22:53 +08:00
2017-11-23 12:33:32 +08:00
while ( itor.hasNext() )
2017-11-22 19:22:53 +08:00
{
QQtWebAccessSession* s1 = itor.next();
2017-11-23 12:33:32 +08:00
if ( s1->getTimer() == timer )
2017-11-22 19:22:53 +08:00
{
s0 = s1;
break;
}
}
2017-11-23 12:33:32 +08:00
if ( !s0 )
return NULL;
return s0->getWebAccessUrl();
}
QQtWebAccessSession* QQtWebAccessSessionManager::getSessionByTimer ( QTimer* timer )
{
QQtWebAccessSession* s0 = NULL;
QListIterator<QQtWebAccessSession*> itor ( m_listWebAccessSession );
2017-11-22 19:22:53 +08:00
2017-11-23 12:33:32 +08:00
while ( itor.hasNext() )
{
QQtWebAccessSession* s1 = itor.next();
if ( s1->getTimer() == timer )
{
s0 = s1;
break;
}
}
2017-11-22 19:22:53 +08:00
2017-11-23 12:33:32 +08:00
return s0;
2017-11-22 19:22:53 +08:00
}
2017-11-23 12:33:32 +08:00
QNetworkReply* QQtWebAccessSessionManager::getReplyHandler ( QQtWebAccessSession* session )
2017-11-22 19:22:53 +08:00
{
2017-11-23 12:33:32 +08:00
if ( !session )
return NULL;
return session->getWebAccessReply();
2017-11-22 19:22:53 +08:00
}
2017-11-23 12:33:32 +08:00
QString QQtWebAccessSessionManager::getRequestUrl ( QQtWebAccessSession* session )
2017-11-22 19:22:53 +08:00
{
2017-11-23 12:33:32 +08:00
if ( !session )
return QString();
return session->getWebAccessUrl();
2017-11-22 19:22:53 +08:00
}
2017-11-23 12:33:32 +08:00
QString QQtWebAccessSessionManager::getSessionName ( QQtWebAccessSession* session )
2017-11-22 19:22:53 +08:00
{
2017-11-23 12:33:32 +08:00
if ( !session )
return QString();
return session->getWebAccessSessionName();
2017-11-22 19:22:53 +08:00
}