Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. "Connection closed/refused" error When I try to send Http Get Request using "QNetworkAccessManager"
QtWS25 Last Chance

"Connection closed/refused" error When I try to send Http Get Request using "QNetworkAccessManager"

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 3 Posters 452 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Chaitra G
    wrote on last edited by
    #1

    Re: "Connection closed" When using QNetworkAccessManager and QTcpServer

    jsulmJ 1 Reply Last reply
    0
    • C Chaitra G

      Re: "Connection closed" When using QNetworkAccessManager and QTcpServer

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Chaitra-G Don't you think you should provide more information?!

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • C Offline
        C Offline
        Chaitra G
        wrote on last edited by
        #3

        since I am first time user its not allow me to post my details immediately.
        please find the details here

        #include <QCoreApplication>
        #include <QDebug>
        #include <QNetworkAccessManager>
        #include <QNetworkRequest>
        #include <QNetworkReply>
        #include <QUrl>
        #include <QUrlQuery>
        #include <main.h>
        #include <iostream>
        #include <QNetworkProxy>

        void sendGetRequest();

        int main(int argc, char *argv[])
        {
        QCoreApplication a(argc, argv);
        sendGetRequest();
        return a.exec();
        }

        //GET Function
        void sendGetRequest()
        {
        QNetworkAccessManager *mgr = new QNetworkAccessManager();

        QString url1 = "http://15.77.38.184/";
        QString url2 = "http://15.77.38.184/cgi-bin/handleJson/shrmem";
        
         QNetworkRequest requestInfo;
        
          requestInfo.setHeader(QNetworkRequest::UserAgentHeader, "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36//");
          requestInfo.setRawHeader("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9");
          requestInfo.setRawHeader("Accept-Encoding","gzip,deflate");
          requestInfo.setRawHeader("Cache-Control","max-age=0");
          requestInfo.setRawHeader("Accept-Launguage","en-US,*");
           requestInfo.setUrl(QUrl(url2));
        
        
          QNetworkProxy proxy;
          proxy.setType(QNetworkProxy::HttpProxy);
          proxy.setHostName("127.0.0.1");
          proxy.setPort(8080);
          mgr->setProxy(proxy);
          QNetworkProxy::setApplicationProxy(proxy);
          QEventLoop eventLoop;
          QNetworkReply *reply = mgr->get(requestInfo);
        
        QObject::connect(reply, SIGNAL(finished()), &eventLoop, SLOT(quit()));
        eventLoop.exec();
        
        if(reply->error() == QNetworkReply::NoError)
        {
            qDebug() << "request http No error";
        }
        else
        {
            qDebug() << "request http handle error here";
            QVariant statusCodev = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
            qDebug("request http found error...: %d %d \n", statusCodev.toInt(), (int)reply->error());
            qDebug(qPrintable(reply->errorString()));
             qDebug() << reply->error();
        }
        
        QByteArray responseByte = reply->readAll();
        qDebug() << responseByte;
        

        }

        This is my code. I am getting empty data everytime in response and connection closed error not able to find the content data. Please suggest me the solution for this?

        even I tried to setProxy not working for me

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi,

          You should connect the error related signals from QNetworkAccessManager.

          Also, are you sure that the request you send is valid ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          C 1 Reply Last reply
          0
          • SGaistS SGaist

            Hi,

            You should connect the error related signals from QNetworkAccessManager.

            Also, are you sure that the request you send is valid ?

            C Offline
            C Offline
            Chaitra G
            wrote on last edited by
            #5

            @SGaist Yes Same request is working with RESTclient and wireshak even in browser I am able to see the data But using qt its receiving empty data.

            "You should connect the error related signals from QNetworkAccessManager." to this

            QObject::connect(reply, SIGNAL(finished()), &eventLoop, SLOT(quit()));

            As I understood i am just sending eventloop until reply->finished signal comes then i will quit the loop

            using reply->readAll i can get the data.

            Please correct me if i doing anything wrong here.

            jsulmJ 1 Reply Last reply
            0
            • C Chaitra G

              @SGaist Yes Same request is working with RESTclient and wireshak even in browser I am able to see the data But using qt its receiving empty data.

              "You should connect the error related signals from QNetworkAccessManager." to this

              QObject::connect(reply, SIGNAL(finished()), &eventLoop, SLOT(quit()));

              As I understood i am just sending eventloop until reply->finished signal comes then i will quit the loop

              using reply->readAll i can get the data.

              Please correct me if i doing anything wrong here.

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @Chaitra-G said in "Connection closed/refused" error When I try to send Http Get Request using "QNetworkAccessManager":

              Please correct me if i doing anything wrong here.

              Can you please connect error related signals to slots like @SGaist suggested and see whether you get any errors?

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved