QNetworkManager and post requests



  • Hi Guys,

    i'm trying to send a post request, via QNetworkManager. This post request should trigger a c2dm push notification to an android phone.

    @
    QUrl header("https://android.clients.google.com/c2dm/send");
    header.addQueryItem("Authorization: GoogleLogin auth",m_authCode);
    QNetworkRequest *req(header);
    req->setHeader(QNetworkRequest::ContentLengthHeader,header.encodedQuery().length());

    QUrl url;
    url.addQueryItem("registration_id",m_pRegCode->text());
    url.addQueryItem("data.payload","data");
    url.addQueryItem("collapse_key","0");
    qDebug("%s",qPrintable(url.toString()));
    qDebug("%s",qPrintable(QString(url.encodedQuery())));
    QByteArray data;
    data = url.encodedQuery();
    m_pPushRep = m_pManager->post(req,data);
    @

    I don't get a finished Signal from the Manager. What am i doing wrong? Has anyone experience with the c2dm service of google?



  • Have you verified that ... ?

    • you are correctly connecting to the finished signal (connect() returns true)
    • you are correctly handling SSL validation errors (by connecting to the sslErrors signal)
    • your request is correctly sent and a response is received (using eg. Wireshark)


  • Hi Lukas,

    Yes i am correctly connected to the finished signal (connect returns true, there is no qDebug message in the shell, and last but not least, before im handling the upper code i am requesting something else via the same Networkaccessmanager.Also i don't think i get ssl errors, cause, as i said im connecting previously also via https, but i didn't connect to the sslError Signal, thats correct.

    My error image has also moved. I think the problem was the wrong header. Im executing the code like this now:

    @
    QString headerStr = QString("Authorization: GoogleLogin auth=") + m_authCode;

    QUrl url;
    url.addQueryItem("registration_id",m_pRegCode->text());
    url.addQueryItem("collapse_key","0");
    url.addQueryItem("data.payload","data");
    qDebug("%s",qPrintable(url.toString()));
    qDebug("%s",qPrintable(QString(url.encodedQuery())));
    QByteArray data;
    data = url.encodedQuery();

    QUrl header("https://android.apis.google.com/c2dm/send");
    QNetworkRequest req(header);
    req.setHeader(QNetworkRequest::ContentTypeHeader,"application/x-www-form-urlencoded;charset=UTF-8");
    req.setHeader(QNetworkRequest::ContentLengthHeader,data.length());
    req.setRawHeader(QByteArray("Authorization"),headerStr.toAscii());

    qDebug("%s",qPrintable(req.url().toString()));
    m_pPushRep = m_pManager->post(req,data);
    @

    With this, i do get an answer from the google servers, but the answer is
    @
    <HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
    <TITLE>302 Moved</TITLE></HEAD><BODY>
    <H1>302 Moved</H1>
    The document has moved
    <A HREF="http://www.google.com">here</A>.
    </BODY></HTML>
    @

    This actually confuses me a little bit. Can anyone provide some help with it?



  • You get a 302 redirect. QNAM does not follow redirections by default, so it's up to you to grab the returned URL and re-send your request. Get the target URL from the returned [[Doc:QNetworkReply]] at attribute QNetworkRequest::RedirectionTargetAttribute.



  • Yeah, but redirecting to www.google.com doesn't make any sense, so i see no point in following it.

    Am i doing something wrong in my code, posted up there?



  • You should ask google why they are sending this redirect.

    If you are able to use Qt 4.8, I would recommend looking into [[Doc:QHttpMultiPart]] which eases posting HTML/HTTP forms significantly.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.