HttpClient.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #include "include/HttpClient.h"
  2. #include "include/httpThreadNum1_global.h"
  3. HttpClient::HttpClient()
  4. {
  5. }
  6. //传文件用的,暂时没有用
  7. bool HttpClient::post(QString strUrl, QString filePath, QString *errString)
  8. {
  9. bool Result = true;
  10. const QUrl url = QUrl::fromUserInput(strUrl);
  11. m_Manager_ = new QNetworkAccessManager(this);
  12. QNetworkRequest network_request;
  13. network_request.setUrl(url);
  14. QFile *file = new QFile(filePath);
  15. file->open(QIODevice::ReadOnly);
  16. QFileInfo info(file->fileName());
  17. QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::RelatedType); //http发送文件
  18. QHttpPart filePart;
  19. filePart.setBodyDevice(file);
  20. file->setParent(multiPart);
  21. filePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant(QString("form-data; name=%1;filename=%2").arg(info.fileName()).arg(info.fileName())));
  22. multiPart->append(filePart);
  23. network_request.setRawHeader("fileName",info.fileName().toLatin1());
  24. QNetworkReply* reply = m_Manager_->post(network_request,multiPart);
  25. multiPart->setParent(reply);
  26. QEventLoop eventLoop;
  27. connect(reply, &QNetworkReply::finished, &eventLoop, &QEventLoop::quit);
  28. eventLoop.exec(QEventLoop::ExcludeUserInputEvents);
  29. QByteArray replyData = reply->readAll();
  30. int statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
  31. QVariant redirectAttr = reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
  32. if (reply->error()
  33. || 300 == statusCode //状态码300 Multiple Choices,既不是错误也不算重定向,应该是qt bug
  34. || !redirectAttr.isNull())
  35. {
  36. *errString = reply->error() ? reply->errorString() : QString("发生重定向(%1),不允许此情况").arg(statusCode);
  37. replyData.clear();
  38. Result = false;
  39. }
  40. file->close();
  41. delete file;
  42. delete multiPart;
  43. reply->deleteLater();
  44. reply = nullptr;
  45. delete m_Manager_;
  46. return Result;
  47. }
  48. //连接服务器,发送和接收数据
  49. //参数:url接口地址,请求信息data,错误信息,接收信息data
  50. bool HttpClient::postQString(QString strUrl, QString strName, QString *errString ,QString *getCardBox)
  51. {
  52. bool Result = true;
  53. const QUrl url = QUrl::fromUserInput(strUrl);
  54. m_Manager_QString = new QNetworkAccessManager(this);
  55. //http的固定请求类型
  56. QNetworkRequest network_request;
  57. network_request.setUrl(url);
  58. QString passQStringJsonTemp=strName;
  59. QByteArray passQStringJson=passQStringJsonTemp.toUtf8();
  60. //使用net请求的格式
  61. network_request.setHeader(QNetworkRequest::ContentTypeHeader, ("application/json"));
  62. network_request.setRawHeader("binform-auth","b602b640be404c8497f0fecc01d1f814");
  63. g_QMutex_token.lock();
  64. network_request.setRawHeader("token",g_qsToken.toUtf8().data()); //将来扩展token使用
  65. g_QMutex_token.unlock();
  66. //使用qt标准post请求
  67. QNetworkReply* reply = m_Manager_QString->post(network_request,passQStringJson);
  68. //循环接收,知道包完整在进行下一步,所以做了一个子线程
  69. //这个循环是一个阻塞
  70. QEventLoop eventLoop;
  71. connect(reply, &QNetworkReply::finished, &eventLoop, &QEventLoop::quit);
  72. eventLoop.exec(QEventLoop::ExcludeUserInputEvents);
  73. QByteArray replyData = reply->readAll();
  74. // qDebug()<<replyData<<"看看打印";
  75. //接收返回的信息data;
  76. *getCardBox=replyData;
  77. int statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
  78. QVariant redirectAttr = reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
  79. // qDebug()<<"error"<<reply->error()<<"状态"<<statusCode<< "重定向"<<!redirectAttr.isNull();
  80. if (reply->error()
  81. || 300 == statusCode //状态码300 Multiple Choices,既不是错误也不算重定向,应该是qt bug
  82. || !redirectAttr.isNull())
  83. {
  84. *errString = reply->error() ? reply->errorString() : QString("发生重定向(%1),不允许此情况").arg(statusCode);
  85. replyData.clear();
  86. Result = false;
  87. qDebug()<<statusCode<<!redirectAttr.isNull()<<reply->error()<<"错误";
  88. }
  89. reply->deleteLater();
  90. reply = nullptr;
  91. delete m_Manager_QString;
  92. return Result;
  93. }