"Connection closed/refused" error When I try to send Http Get Request using "QNetworkAccessManager"
-
-
@Chaitra-G Don't you think you should provide more information?!
-
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
-
Hi,
You should connect the error related signals from QNetworkAccessManager.
Also, are you sure that the request you send is valid ?
-
@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.
-
@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?