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. QNetworkManager and post requests
QtWS25 Last Chance

QNetworkManager and post requests

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 Posters 6.0k 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.
  • M Offline
    M Offline
    mm7490
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • L Offline
      L Offline
      lgeyer
      wrote on last edited by
      #2

      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)
      1 Reply Last reply
      0
      • M Offline
        M Offline
        mm7490
        wrote on last edited by
        #3

        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?

        1 Reply Last reply
        0
        • G Offline
          G Offline
          goetz
          wrote on last edited by
          #4

          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.

          http://www.catb.org/~esr/faqs/smart-questions.html

          1 Reply Last reply
          0
          • M Offline
            M Offline
            mm7490
            wrote on last edited by
            #5

            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?

            1 Reply Last reply
            0
            • G Offline
              G Offline
              goetz
              wrote on last edited by
              #6

              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.

              http://www.catb.org/~esr/faqs/smart-questions.html

              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