Results 1 to 4 of 4

Thread: Program crashes on QNetworkAccessManager::post() request.

  1. #1
    Join Date
    Jul 2012
    Posts
    201
    Thanks
    26
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Program crashes on QNetworkAccessManager::post() request.

    Hi there guys, please see my code below. The program crashes on "line 93". The first QNetworkAccessManager::get() request on line 17 works just fine but for some reason the program crashes on the second request on line 93. Please see debug output attached at the bottom of the post.
    Qt Code:
    1. #include "cpt_rfqs.h"
    2.  
    3. CPT_RFQs::CPT_RFQs()
    4. {
    5. connect(this, &CPT_RFQs::downloadedSiteData, this, &CPT_RFQs::filterRFQs);
    6. }
    7.  
    8. void CPT_RFQs::start_downloading()
    9. {
    10. getRFQinfo();
    11. }
    12.  
    13. void CPT_RFQs::getRFQinfo()
    14. {
    15. rfqNetworkManager = new QNetworkAccessManager(this);
    16. connect(rfqNetworkManager, &QNetworkAccessManager::sslErrors, this, &CPT_RFQs::sslConnectionErrors);
    17. rfqNetworkManager->get(QNetworkRequest(QUrl("http://web1.capetown.gov.za/web1/ProcurementPortal/RFQ/")));
    18.  
    19. connect(rfqNetworkManager, &QNetworkAccessManager::finished, this, [this](QNetworkReply *homePageReply){
    20. std::string html_buffer = static_cast<std::string>(homePageReply->readAll());
    21. xmlDoc *doc = htmlReadDoc((xmlChar*)html_buffer.c_str(), NULL, NULL, HTML_PARSE_RECOVER|HTML_PARSE_NOERROR|HTML_PARSE_NOWARNING);
    22.  
    23. xmlNode *node = xmlDocGetRootElement(doc);
    24. xmlpp::Element *root = new xmlpp::Element(node);
    25. auto node_set = root->find("/html/body/div/div/section/form/div/div/table/tbody");
    26.  
    27. auto containerDiv = dynamic_cast<const xmlpp::Element*>(node_set[0]);
    28. auto elementList = containerDiv->get_children("tr");
    29.  
    30. QSet<QString> siteDataList;
    31. for(const auto& child : elementList)
    32. {
    33. auto tableDiv = dynamic_cast<xmlpp::Element*>(child);
    34. auto tableRows = tableDiv->get_children("td");
    35.  
    36. int i = 0;
    37. QStringList dataRecord;
    38. for(const auto& data : tableRows)
    39. {
    40. auto tdElement = dynamic_cast<xmlpp::Element*>(data);
    41. if(i == 7)
    42. {
    43. auto aNode = tdElement->get_children("a");
    44. auto hrefEle = dynamic_cast<const xmlpp::Element*>(aNode.front());
    45. dataRecord.append(QString::fromUtf8(static_cast<std::string>(hrefEle->get_attribute_value("href")).c_str()).simplified());
    46. }
    47. else
    48. {
    49. std::string stdStr = static_cast<std::string>(tdElement->get_child_text()->get_content()).c_str();
    50. dataRecord.append(QString::fromUtf8(stdStr.c_str()).simplified());
    51. }
    52. i++;
    53. }
    54. QStringList tempContainer;
    55. tempContainer << dataRecord.at(0) << dataRecord.at(6) << dataRecord.at(4) << dataRecord.at(7);
    56. siteDataList.insert(tempContainer.join(","));
    57. dataRecord.clear();
    58. tempContainer.clear();
    59. }
    60. xmlFreeDoc(doc);
    61. emit downloadedSiteData(siteDataList);
    62. homePageReply->deleteLater();
    63. });
    64. }
    65.  
    66. void CPT_RFQs::filterRFQs(QSet<QString> siteInfo)
    67. {
    68. QFile rfqRegister("/home/ayanda/Desktop/TenderBot_Utilis/cpt/rfq_s/previosly_downloaded_rfq_s.csv");
    69. if(!rfqRegister.open(QFile::ReadWrite | QFile::Text))
    70. qDebug() << "RFQ file failed to open" <<endl;
    71.  
    72. QTextStream inStream(&rfqRegister);
    73. QSet<QString> registerDataSet;
    74. while(!inStream.atEnd())
    75. registerDataSet.insert(inStream.readLine().simplified());
    76.  
    77. homePageDataList = siteInfo.subtract(registerDataSet);
    78. if(!homePageDataList.isEmpty())
    79. loginToPage();
    80. else
    81. emit noNewRFQs();
    82. }
    83.  
    84. void CPT_RFQs::loginToPage()
    85. {
    86. QUrlQuery logins;
    87. logins.addQueryItem("UserName","theusername");
    88. logins.addQueryItem("Password","thepassword");
    89.  
    90. QNetworkRequest request(QUrl("http://web1.capetown.gov.za/web1/ProcurementPortal/Account/LogOn"));
    91. request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
    92. connect(rfqNetworkManager, &QNetworkAccessManager::finished, this, &CPT_RFQs::get_RFQ);
    93. rfqNetworkManager->post(request, logins.toString(QUrl::FullyEncoded).toUtf8());
    94. }
    95.  
    96. void CPT_RFQs::get_RFQ(QNetworkReply* reply)
    97. {
    98. Q_UNUSED(reply);
    99. foreach(const QString &value, homePageDataList)
    100. qDebug() << value <<endl;
    101. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include <QTextStream>
    2. #include <QUrlQuery>
    3. #include <QStringList>
    4. #include <QList>
    5. #include <QDebug>
    6. #include <string>
    7. #include <QSet>
    8. #include "page.h"
    9.  
    10. class CPT_RFQs : public Page
    11. {
    12. Q_OBJECT
    13. public:
    14. CPT_RFQs();
    15. void start_downloading();
    16.  
    17. signals:
    18. void downloadedSiteData(QSet<QString>);
    19. void noNewRFQs();
    20. void login();
    21. public slots:
    22. //void sslConnectionErrors(QNetworkReply *reply, const QList<QSslError> &errors);
    23. void getRFQinfo();
    24. void filterRFQs(QSet<QString>);
    25. void loginToPage();
    26. void get_RFQ(QNetworkReply*);
    27.  
    28. private:
    29. QSet<QString> homePageDataList;
    30. QNetworkAccessManager *rfqNetworkManager;
    31. };
    32.  
    33. #endif // CPT_RFQS_H
    To copy to clipboard, switch view to plain text mode 

    Attachment 12384
    Attached Images Attached Images
    Last edited by anda_skoa; 16th March 2017 at 12:05.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Program crashes on QNetworkAccessManager::post() request.

    The screenshot and your description don't match.

    The screenshot says line 27, your comment says line 93.

    Cheers,
    _

  3. #3
    Join Date
    Jul 2012
    Posts
    201
    Thanks
    26
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Program crashes on QNetworkAccessManager::post() request.

    thank you for your response anda_skoa. I'm sorry that was my mistake, the line is "27" as per the screenshot, it's just that when I comment out line 93, the program doesn't crash but of course in that case it does work as required.

  4. #4
    Join Date
    Jul 2012
    Posts
    201
    Thanks
    26
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Program crashes on QNetworkAccessManager::post() request.

    I was able to solve this problem. The problem was that I connected the QNetworkAccessManager::finished() signal twice in line 16 and 92. The solution is to assign the QNetworkAccessManager::get() (or QNetworkAccessManager:ost() ) to pointer of QNetworkReply and then connect the finished signal for QNetworkReply to a slot.

Similar Threads

  1. Replies: 2
    Last Post: 9th March 2017, 14:36
  2. QT5 http post request
    By marty.marty in forum Qt Programming
    Replies: 0
    Last Post: 14th January 2015, 12:44
  3. Replies: 1
    Last Post: 6th February 2013, 13:18
  4. POST request - getting cookie
    By Trok in forum Qt Programming
    Replies: 3
    Last Post: 17th September 2012, 00:32
  5. Request ID of QNetworkaccessmanager get and post request
    By dineshkumar in forum Qt Programming
    Replies: 2
    Last Post: 4th February 2011, 22:56

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.