Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    QNetworkManager and post requests

    General and Desktop
    3
    6
    5604
    Loading More Posts
    • 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
      mm7490 last edited by

      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 Reply Quote 0
      • L
        lgeyer last edited by

        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 Reply Quote 0
        • M
          mm7490 last edited by

          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 Reply Quote 0
          • G
            goetz last edited by

            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 Reply Quote 0
            • M
              mm7490 last edited by

              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 Reply Quote 0
              • G
                goetz last edited by

                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 Reply Quote 0
                • First post
                  Last post