PDA

View Full Version : Replacing QHttp.get() with QNetworkAccessManager



Grisu
31st December 2012, 12:01
Hi,

I've just installed QT 5.0 and realized that QHttp has been removed.
I'm using it for downloading an XML-reply from Amazon Webservices.
I tried to redesign my code.
With a simple Adress "www.google.de" it's working.

Here is a code snipped:


...
m_netManager = new QNetworkAccessManager(this);
QUrl url("http://webservices.amazon.de/onca/xml?AWSAccessKeyId=1HXFTRJCMBZPF4PGQ2G2&AssociateTag=wwwsealsoftde-20&IdType=EAN&ItemId=5051890049414&Operation=ItemLookup&ResponseGroup=Images%2CItemAttributes%2CRequest%2C EditorialReview&SearchIndex=All&Service=AWSECommerceService&Timestamp=2012-12-31T11%3A03%3A27Z&Signature=M6P6gs%2FXQ6pUnK8ZqZq9LThV7Tf%2FM5J6GYfI 8Qdgt3Q%3D");
m_netRequest.setUrl(url);
m_netReplyData = m_netManager->get(m_netRequest);
connect(m_netReplyData, SIGNAL(error(QNetworkReply::NetworkError)),
this, SLOT(slotErrorData(QNetworkReply::NetworkError)));
...


void HttpData::slotErrorData(QNetworkReply::NetworkErro r code){
qDebug()<<"HttpData::slotErrorData";
if(code == QNetworkReply::OperationCanceledError){
...
}else{
qDebug()<<"Errorstring: "<< m_netReplyData->errorString();
qDebug()<<"Errorcode"<<code;


This is the Errorstring:
http://webservices.amazon.de/onca/xml?AWSAccessKeyId=1HXFTRJCMBZPF4PGQ2G2&AssociateTag=wwwsealsoftde-20&IdType=EAN&ItemId=5051890049414&Operation=ItemLookup&ResponseGroup=Images%2CItemAttributes%2CRequest%2C EditorialReview&SearchIndex=All&Service=AWSECommerceService&Timestamp=2012-12-31T11%3A03%3A27Z&Signature=M6P6gs%2FXQ6pUnK8ZqZq9LThV7Tf%2FM5J6GYfI 8Qdgt3Q%3D - server replied: Bad Request

If this ist not enough code I can write a small sample programm.

Can someone help me with this Problem, with QHttp.get() this code was working?

Greetings,

Grisu

Hi,

I've just installed QT 5.0 and realized that QHttp has been removed.
I'm using it for downloading an XML-reply from Amazon Webservices.
I tried to redesign my code.
With a simple Adress "www.google.de" it's working.

Here is a code snipped:


...
m_netManager = new QNetworkAccessManager(this);
QUrl url("http://webservices.amazon.de/onca/xml?AWSAccessKeyId=1HXFTRJCMBZPF4PGQ2G2&AssociateTag=wwwsealsoftde-20&IdType=EAN&ItemId=5051890049414&Operation=ItemLookup&ResponseGroup=Images%2CItemAttributes%2CRequest%2C EditorialReview&SearchIndex=All&Service=AWSECommerceService&Timestamp=2012-12-31T11%3A03%3A27Z&Signature=M6P6gs%2FXQ6pUnK8ZqZq9LThV7Tf%2FM5J6GYfI 8Qdgt3Q%3D");
m_netRequest.setUrl(url);
m_netReplyData = m_netManager->get(m_netRequest);
connect(m_netReplyData, SIGNAL(error(QNetworkReply::NetworkError)),
this, SLOT(slotErrorData(QNetworkReply::NetworkError)));
...


void HttpData::slotErrorData(QNetworkReply::NetworkErro r code){
qDebug()<<"HttpData::slotErrorData";
if(code == QNetworkReply::OperationCanceledError){
...
}else{
qDebug()<<"Errorstring: "<< m_netReplyData->errorString();
qDebug()<<"Errorcode"<<code;


This is the Errorstring:
http://webservices.amazon.de/onca/xml?AWSAccessKeyId=1HXFTRJCMBZPF4PGQ2G2&AssociateTag=wwwsealsoftde-20&IdType=EAN&ItemId=5051890049414&Operation=ItemLookup&ResponseGroup=Images%2CItemAttributes%2CRequest%2C EditorialReview&SearchIndex=All&Service=AWSECommerceService&Timestamp=2012-12-31T11%3A03%3A27Z&Signature=M6P6gs%2FXQ6pUnK8ZqZq9LThV7Tf%2FM5J6GYfI 8Qdgt3Q%3D - server replied: Bad Request

If this ist not enough code I can write a small sample programm.

Can someone help me with this Problem, with QHttp.get() this code was working?

Greetings,

Grisu

Always the same....
Im working on this for two days. And 30 minutes after this post I got it :-)


m_netRequest.setUrl(QUrl("http://"+strUrl.host()+strUrl.path()));
m_netReplyData = m_netManager->post(m_netRequest,strUrl.encodedQuery());

jsuppe
1st February 2013, 02:25
I have the same problem and I am kind of stuck.

My situation is also using with QNetworkAccessManager using amazon S3 Url. The QNetworkReply::errorString() coming back is:

Error downloading http://mybucket.s3.amazonaws.com/apps/xyz.jpg?AWSAccessKeyId=12345&Expires=12345&Signature=Df7xvlcreMpA%252BhJRkYhtMSQopo4%253D - server replied: Forbidden


BUT- the URL that is set on QNetworkReply and QNetworkRequest is different:

http://mybucket.s3.amazonaws.com/apps/xyz.jpg?AWSAccessKeyId=12345&Expires=12345&Signature=Df7xvlcreMpA%252BhJRkYhtMSQopo4%3D


Notice the %253D at the end of the URL in the QNetworkReply::errorString()

ChrisW67
1st February 2013, 02:32
The QUrl() constructor you are both using:


QUrl::QUrl ( const QString & url )

Constructs a URL by parsing url. url is assumed to be in human readable representation, with no percent encoding. QUrl will automatically percent encode all characters that are not allowed in a URL. You are both passing encoded strings, which then get encoded a second time.

The docs also tell you how to remedy this with QUrl::fromEncoded(), or you could not encode it in the first place, or build it from components using QUrl methods.