PDA

View Full Version : unable to pass the data using http localhost port (QUrl url("http://localhost:445");)



gowreesh
12th May 2015, 04:03
Hi Everyone,

I am fresher, trying to develop a code for a my first project , need some help

I am writting a program , where am trying to pass the xml file body using QHttpMultiPart.
and the received data from the from the QNetworkAccessManager , i wanted to print and see that the data is coming from localhost port properly or not.
Please find the below code i have developed

Main.c


int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);

Uploader obj;
QString xmllink = "C:/Users/ramachandrug/Documents/QT/XML_pharse/rtioInterface.xml";
obj.upload(xmllink);

return a.exec();
}



XmlComms.cpp

void Uploader::upload(QString xmlFilePath)
{

qDebug() << "Upload Starting";
QFileInfo fileInfo(xmlFilePath);
qDebug() << "file path is :" <<fileInfo.absoluteFilePath();
qDebug() << "file size is :" << fileInfo.size();

QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);

QHttpPart filePart;
filePart.setHeader(QNetworkRequest::ContentDisposi tionHeader, QVariant("HTTP/1.1 200 OK"));
filePart.setHeader(QNetworkRequest::ContentTypeHea der, QVariant("text/xml; charset=utf-8"));

QFile *file = new QFile(xmlFilePath);
if ( !file->exists() )
{
qDebug() << "File Does not exist";
}

file->open(QIODevice::ReadOnly);
filePart.setBodyDevice(file);
file->setParent(multiPart); // we cannot delete the file now, so delete it with the multiPart

multiPart->append(filePart);

QUrl url("http://localhost:445");
qDebug() << "Host:" << url.host();
qDebug() << "Port:" << url.port();


QNetworkRequest request(url);

pManager = new QNetworkAccessManager();

pReply = pManager->post(request, multiPart);
multiPart->setParent(pReply);

//qDebug() <<"XML body is: "<<pReply->readAll();


QEventLoop *pELoop = new QEventLoop();


QObject::connect(pManager, SIGNAL(finished(QNetworkReply* )),this, SLOT(replyFinished(QNetworkReply*)));

pELoop->exec();


}



/**
* @brief Uploader::replyFinished
*/

void Uploader::replyFinished(QNetworkReply *reply)
{

QByteArray data = reply->readAll();
qDebug() << "data is::"<< data;
qDebug() << "Upload completed";

uploadInProgress = false;
if ( reply->error() > 0 )
{
qDebug() << "Error occured: " << reply->error() << " : " << reply->errorString();
}
else
{
qDebug() << "Upload success";
}

//_dataArrived(data); //passing the extracted XML body
reply->deleteLater();


}



When i run this code, am getting the below output

upload starting
file path is : C:/Users/ramachandrug/Documents/QT/XML_pharse/rtioInterface.xml

file size is 35440
Host : localhost
port: 445
data is :: "" //here no output is coming, empty byte array is coming
upload completed
Error occured: 99 : "unknown error" // for to fix this error, i installed the openssl also but still am getting this error



Am running this QT project in my host environment only.

Anyone please let me know , why am not able to print this xml body here. I am stuck here from last 2days.
please help me to fix this issue.

Thanks
Gowreesh

wysota
12th May 2015, 08:24
What is running on port 445? Are you receiving the data there?

gowreesh
12th May 2015, 08:45
thanks for the response.

Randomly I have selected one among the List of open ports and listening services(by entering the command "netstat -an"). and the port 445 is a TCP proto .
and yes am trying to receive the data from this port. and the same received data am trying print using:
QByteArray data = reply->readAll();
qDebug() << "data is::"<< data;

wysota
12th May 2015, 08:53
Randomly I have selected one among the List of open ports and listening services(by entering the command "netstat -an"). and the port 445 is a TCP proto .
If there is no HTTP service listening there then how do you expect it to accept your data and send something back?

ChrisW67
12th May 2015, 22:06
You are also sending very specific request information, obviously designed for specific service, to a random service. Even if you got a reply it is very unlikely to be useful.

When you find the real service to make the request on you may need to set the content type on the QNetworkRequest match the multi-part data in your payload.

jefftee
13th May 2015, 15:40
And lastly, ports < 1024 are restricted ports and typically require administrative authority to listen on restricted ports. Choose something > 1024 and you won't have any special access requirements.

gowreesh
25th May 2015, 08:59
Thank you everyone for your response.
from all of your inputs , I have developed a below piece of code.
Expecting one more help from you all .
Here am trying to read the XML data from the localhost port http://127.0.0.1:8888 .
Below is the code which i have doubt

/* main */

int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);

Uploader obj;
QString xmllink = "C:/Users/ramachandrug/Desktop/XML_Pharse/XML/SC_FORM_A_AUTHORISEDResponse.xml";
obj.upload(xmllink);
Server server;
return a.exec();
}


/* server.cpp */

#include <QtNetwork>
#include <QObject>
#include <QTcpServer>
#include <QTcpSocket>
// server.cc
#include "server.h"

Server::Server()
{
qDebug() << "server initiated";
QHostAddress hostadd(QHostAddress::Any);
connect(&server, SIGNAL(newConnection()), this, SLOT(acceptConnection()));

server.listen(hostadd , 8888);
}

Server::~Server()
{
server.close();
}

void Server::acceptConnection()
{

qDebug() << "server initiated 2";
client = server.nextPendingConnection();

connect(client, SIGNAL(readyRead()), this, SLOT(startRead()));
}

void Server::startRead()
{

qDebug() << "server started";
char buffer[1024] = {0};
client->read(buffer, client->bytesAvailable());
qDebug() << "value read from the port is :" << buffer;
//_handleIncomingData(buffer); // here it goes further for a separate system
}



/*client.cpp */

#include <QUrl>
#include <QFileInfo>
#include <QMimeDatabase>
#include <QHttpMultiPart>
#include <QDebug>
#include <QEventLoop>
#include <QNetworkReply>
#include <QDomDocument>

#include "server.h"

#include "XmlComms.h"


void Uploader::upload(QString xmlFilePath)
{

qDebug() << "Upload Starting";
QFileInfo fileInfo(xmlFilePath);
qDebug() << "file path is :" <<fileInfo.absoluteFilePath();
qDebug() << "file size is :" << fileInfo.size();

QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);

QHttpPart filePart;
filePart.setHeader(QNetworkRequest::ContentTypeHea der, QVariant("text/xml"));
filePart.setHeader(QNetworkRequest::ContentDisposi tionHeader, QVariant("form-data; filename=\"text\""));

QFile *file = new QFile(xmlFilePath);

if ( !file->exists() )
{
qDebug() << "File Does not exist";
}

file->open(QIODevice::ReadOnly);
filePart.setBodyDevice(file);
file->setParent(multiPart); // we cannot delete the file now, so delete it with the multiPart

multiPart->append(filePart);

QUrl url("http://127.0.0.1:8888");
qDebug() << "Host:" << url.host();
qDebug() << "Port:" << url.port();
QNetworkRequest request(url);

pManager = new QNetworkAccessManager();
pReply = pManager->post(request, multiPart);
multiPart->setParent(pReply);

//pELoop = new QEventLoop();

QObject::connect(pReply, SIGNAL(finished( )),this, SLOT(replyFinished()));

//pELoop->exec(); // here is the problem

}


void Uploader::replyFinished()
{

//qDebug() << "data is::"<< pReply->readAll();;
qDebug() << "Upload completed";

if ( pReply->error() > 0 )
{
qDebug() << "Error occured: " << pReply->error() << " : " << pReply->errorString();
}
else
{
qDebug() << "Upload success";
}

pReply->deleteLater();

pELoop->exit();

}


Issue is :
Suppose if I don't include pELoop->exec() in a code, then I can receive the XML data in the localhost port .
and here the function "replyFinished" is not getting invoked, it is because the signal finished() will not be emitted. and
i dont know why it is not getting emitted.
Here is the output below:

Upload Starting
file path is : "C:/Users/ramachandrug/Desktop/XML_Pharse/XML/SC_FORM_A_AUTHORISE
DResponse.xml"
file size is : 650
Host: "127.0.0.1"
Port: 8888
server initiated
server initiated 2
server started
value read from the port is : POST / HTTP/1.1
Content-Type: multipart/form-data; boundary="boundary_.oOo._MjI5Mjc=NzEyMQ==MTk0
MTY="
MIME-Version: 1.0
Content-Length: 815
Connection: Keep-Alive
Accept-Encoding: gzip, deflate
Accept-Language: en-AU,*
User-Agent: Mozilla/5.0
Host: 127.0.0.1:8888


server initiated 2
server started
value read from the port is : POST / HTTP/1.1 /* 2 times this 10 lines are coming in the output*/
Content-Type: multipart/form-data; boundary="boundary_.oOo._MjI5Mjc=NzEyMQ==MTk0
MTY="
MIME-Version: 1.0
Content-Length: 815
Connection: Keep-Alive
Accept-Encoding: gzip, deflate
Accept-Language: en-AU,*
User-Agent: Mozilla/5.0
Host: 127.0.0.1:8888

--boundary_.oOo._MjI5Mjc=NzEyMQ==MTk0MTY=
Content-Type: text/xml
Content-Disposition: form-data; filename="text"

HTTP/1.1 200 OK
Server: gSOAP/2.7
Content-Type: text/xml; charset=utf-8
Content-Length: 506
Connection: close

<?xml version="1.0" encoding="UTF-8" ?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xm
lns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://
www.w3.org/2001/XMLSchema"
xmlns:ns1="http://interfaces.atms.org/AMS-TCS/" xmlns:ns2="http://interf
aces.atms.org/TCS-AMS/">
<SOAP-ENV:Body>
<ns1:SC_FORM_A_AUTHORISEDResponse>
<out>true</out>
</ns1:SC_FORM_A_AUTHORISEDResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>



But here in the above code if I include the pELoop->exec() until finished signal is emitted, then no output comes out.
and network reply returns connection refused.
here is the output below:

Upload Starting
file path is : "C:/Users/ramachandrug/Desktop/XML_Pharse/XML/SC_FORM_A_AUTHORISE
DResponse.xml"
file size is : 650
Host: "127.0.0.1"
Port: 8888
Upload completed
Error occured: 1 : "Connection refused"
server initiated
server initiated 2
----


Requesting you to please tell me why it is saying connection refused . and why those 10 lines of output is printing twice
and does it creates a problem.

anda_skoa
25th May 2015, 09:13
Requesting you to please tell me why it is saying connection refused

Look at the order in which you create the two objects in main().

Cheers,
_

gowreesh
26th May 2015, 04:15
Look at the order in which you create the two objects in main().

Cheers,
_

Hi,
Thanks for the reply.
I have Updated the Main() function as mentioned below.

int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);

QString xmllink = "C:/Users/ramachandrug/Desktop/XML_Pharse/XML/SC_FORM_A_AUTHORISEDResponse.xml";

Server server;


Uploader obj;


obj.upload(xmllink);

return a.exec();
}


After changing the order in the main() function I am getting 2 outputs randomly,for the same build .

Output 1:

server initiated
Upload Starting
file path is : "C:/Users/ramachandrug/Desktop/XML_Pharse/XML/SC_FORM_A_AUTHORISE
DResponse.xml"
file size is : 650
Host: "127.0.0.1"
Port: 8888
server initiated 2
server started
value read from the port is : POST / HTTP/1.1
Content-Type: multipart/form-data; boundary="boundary_.oOo._MTc4MjY=Mjg3NjU=ODI0
Ng=="
MIME-Version: 1.0
Content-Length: 815
Connection: Keep-Alive
Accept-Encoding: gzip, deflate
Accept-Language: en-AU,*
User-Agent: Mozilla/5.0
Host: 127.0.0.1:8888


server initiated 2
server started
value read from the port is : POST / HTTP/1.1
Content-Type: multipart/form-data; boundary="boundary_.oOo._MTc4MjY=Mjg3NjU=ODI0
Ng=="
MIME-Version: 1.0
Content-Length: 815
Connection: Keep-Alive
Accept-Encoding: gzip, deflate
Accept-Language: en-AU,*
User-Agent: Mozilla/5.0
Host: 127.0.0.1:8888


Upload completed
Error occured: 2 : "Connection closed"




Output 2:


server initiated
Upload Starting
file path is : "C:/Users/ramachandrug/Desktop/XML_Pharse/XML/SC_FORM_A_AUTHORISE
DResponse.xml"
file size is : 650
Host: "127.0.0.1"
Port: 8888
server initiated 2
server started
value read from the port is : POST / HTTP/1.1
Content-Type: multipart/form-data; boundary="boundary_.oOo._MTc1NTI=NzkzMw==OTAx
NA=="
MIME-Version: 1.0
Content-Length: 815
Connection: Keep-Alive
Accept-Encoding: gzip, deflate
Accept-Language: en-AU,*
User-Agent: Mozilla/5.0
Host: 127.0.0.1:8888

--boundary_.oOo._MTc1NTI=NzkzMw==OTAxNA==
Content-Type: text/xml
Content-Disposition: form-data; filename="text"

HTTP/1.1 200 OK
Server: gSOAP/2.7
Content-Type: text/xml; charset=utf-8
Content-Length: 506
Connection: close

<?xml version="1.0" encoding="UTF-8" ?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xm
lns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://
www.w3.org/2001/XMLSchema"
xmlns:ns1="http://interfaces.atms.org/AMS-TCS/" xmlns:ns2="http://interf
aces.atms.org/TCS-AMS/">
<SOAP-ENV:Body>
<ns1:SC_FORM_A_AUTHORISEDResponse>
<out>true</out>
</ns1:SC_FORM_A_AUTHORISEDResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
--b►?`♥☻


For the output 2 : After printing above output it will say XML.exe has stopped working. Please find the snapshot also below

11173

Requesting you to please help out for to resolve this issue.

anda_skoa
26th May 2015, 08:06
Well, the stack trace at the time of the crash would probably tell you where it crashes.

My guess is your unchecked memory buffer access here


char buffer[1024] = {0};
client->read(buffer, client->bytesAvailable());


Cheers,
_

ChrisW67
26th May 2015, 11:20
QByteArray is your friend

gowreesh
27th May 2015, 05:40
Well, the stack trace at the time of the crash would probably tell you where it crashes.

My guess is your unchecked memory buffer access here


char buffer[1024] = {0};
client->read(buffer, client->bytesAvailable());


Cheers,
_

Thanks for the reply...

I have modified the code with below statement, no crashes are happening now. and it works fine.

QByteArray buffer;
buffer = client->read(client->bytesAvailable());

But the issue is still the network reply error is "Connection closed" only is coming(from the below code). I am unable to find how the remote server closed the connection prematurely, before the entire reply was received and processed.

pReply = pManager->post(request, multiPart);

I tried changing the localhost port also but still this error is coming.

gowreesh
28th May 2015, 06:30
I will create a New thread with proper explanation and code snippet .... Thank you

jefftee
29th May 2015, 04:30
But the issue is still the network reply error is "Connection closed" only is coming(from the below code). I am unable to find how the remote server closed the connection prematurely, before the entire reply was received and processed.
Look at the headers returned by the server that you're communicating with. Specifically, the following header returned by the server:



Connection: close


This is the server telling you that the http connection will be closed after completion of the response.