bool HttpClient::get(QNetworkRequest request)
{
QNetworkReply *reply;
bool success = false;
request.setPriority(QNetworkRequest::HighPriority);
//向networkRequest里面添加公用的头
for (QMap<QByteArray, QByteArray>::const_iterator i = m_commonHeaders.constBegin(); i != m_commonHeaders.constEnd(); ++i)
{
if (!request.hasRawHeader(i.key()))
{
request.setRawHeader(i.key(), i.value());
}
}
//输出调试信息
qDebug() << "GET: " + request.url().toEncoded();
qDebug() << QString("Send Headers: ");
QList<QByteArray> headerList = request.rawHeaderList();
foreach(QByteArray header, headerList)
qDebug() << header + ":" + request.rawHeader(header);
QList<QNetworkCookie> cookieList = m_networkAccessManager->cookieJar()->cookiesForUrl(request.url());
qDebug() << "send cookies :";
foreach (QNetworkCookie cookie, cookieList)
qDebug() << cookie.name() << "=" << cookie.value();
reply = m_networkAccessManager->get(request);
processReply(reply);
if (m_error == QNetworkReply::NoError)
success = true;
else if (m_error == QNetworkReply::TimeoutError)
success = false;
return success;
}
void HttpClient::processReply(QNetworkReply *reply)
{
//下载进度显示
connect(reply, SIGNAL(downloadProgress(qint64,qint64)), this, SLOT(slotDownLoadProcess(qint64,qint64)));
//启用事件循环等待reply返回数据之后再进行下一步操作
connect(reply, SIGNAL(finished()), m_eventLoop, SLOT(quit()));
m_eventLoop->exec();
//所有状态重置
m_httpStatusCode = 0;
m_data.clear();
m_text.clear();
m_httpStatusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
m_error = reply->error();
m_errorString = reply->errorString();
m_data = reply->readAll();
//解决读取到的中文字符乱码问题
QTextCodec *codec = 0;
if(!codec)
codec = QTextCodec::codecForHtml(m_data, QTextCodec::codecForName(m_defaultCharset));
if(codec)
m_text = codec->toUnicode(m_data);
//输出调试信息
qDebug() << QString("Status Code: %1").arg(m_httpStatusCode);
qDebug() << QString("Error Code: %1").arg(m_error);
qDebug() << QString("Error String: %1").arg(m_errorString);
//qDebug() << QString("text: %1").arg(m_text);
delete reply;
}
bool HttpClient::post(QNetworkRequest request, const QByteArray &data)
{
QNetworkReply *reply;
bool success = false;
request.setPriority(QNetworkRequest::HighPriority);
//向networkRequest里面添加公用的头
for (QMap<QByteArray, QByteArray>::const_iterator i = m_commonHeaders.constBegin(); i != m_commonHeaders.constEnd(); ++i)
{
if (!request.hasRawHeader(i.key()))
{
request.setRawHeader(i.key(), i.value());
}
}
//输出调试信息
qDebug() << "POST " + request.url().toEncoded();
qDebug() << "Content: " + data;
qDebug() << QString("Send Headers:");
QList<QByteArray> headerList = request.rawHeaderList();
foreach (QByteArray header, headerList)
qDebug() << header + ": " + request.rawHeader(header);
QList<QNetworkCookie> cookieList = m_networkAccessManager->cookieJar()->cookiesForUrl(request.url());
qDebug() << "send cookies :";
foreach (QNetworkCookie cookie, cookieList)
qDebug() << cookie.name() << "=" << cookie.value();
reply = m_networkAccessManager->post(request, data);
processReply(reply);
if (m_error == QNetworkReply::NoError)
success = true;
else if (m_error == QNetworkReply::TimeoutError)
success = false;
else
success = false;
return success;
}
bool HttpClient::post(QNetworkRequest request, QHttpMultiPart *multiPart)
{
QNetworkReply *reply;
bool success = false;
request.setPriority(QNetworkRequest::HighPriority);
//向networkRequest里面添加公用的头
for (QMap<QByteArray, QByteArray>::const_iterator i = m_commonHeaders.constBegin(); i != m_commonHeaders.constEnd(); ++i)
{
if (!request.hasRawHeader(i.key()))
{
request.setRawHeader(i.key(), i.value());
}
}
//输出调试信息
qDebug() << "POST " + request.url().toEncoded();
qDebug() << "Content: HttpMultiPart";
qDebug() << QString("Send Headers:");
QList<QByteArray> headerList = request.rawHeaderList();
foreach (QByteArray header, headerList)
qDebug() << header + ": " + request.rawHeader(header);
QList<QNetworkCookie> cookieList = m_networkAccessManager->cookieJar()->cookiesForUrl(request.url());
qDebug() << "send cookies :";
foreach (QNetworkCookie cookie, cookieList)
qDebug() << cookie.name() << "=" << cookie.value();
reply = m_networkAccessManager->post(request, multiPart);
processReply(reply);
if (m_error == QNetworkReply::NoError)
success = true;
else if (m_error == QNetworkReply::TimeoutError)
success = false;
else
success = false;
return success;
}