Qt Forum

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

    Can not get the header(QNetworkRequest::ContentLengthHeader) with a post request

    General and Desktop
    2
    7
    4381
    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.
    • Z
      zhangjunhua last edited by

      i want to a Progressbar bar, so i need to know the "Content-Length" in the header while the data is coming,but when i post my http request, i find that i can not get the QNetworkRequest::ContentLengthHeader using the funiton below:

      @ QVariant Variant2 = iReply->header(QNetworkRequest::ContentLengthHeader);//there is a invalid value

      int ContentLength =  Variant2.toInt();
      
      
      qDebug()<< "ContentLengthHeader:"  + Variant2.toString();@
      

      i make sure the the header of "Content-Length" is exsit while i using the tool named smsniff.exe
      the headers in smsniff.exe below:
      bq. HTTP/1.1 200 OK
      Server: Yaws 1.95
      Date: Fri, 12 Apr 2013 08:32:38 GMT
      Content-Length: 6023
      Content-Type: application/octet-stream
      Content-Encoding: gzip

      so i think it is a bug in QT 4.8.1 and QT 5.0.1

      there is my code below:

      @ QNetworkRequest request;

          request.setUrl("http://www.baidu.com");
      
      
          request.setHeader(QNetworkRequest::ContentTypeHeader,"application/x-www-form-urlencoded");
      
      
          iReply = iNetworkAccessManager.post(request, QByteArray("1234"));
      
      
      connect(iReply, SIGNAL(metaDataChanged()),this, SLOT(notifymetaDataChanged()));@
      

      .........................

      @void QHttpEngine::notifymetaDataChanged()
      {

      QVariant Variant2 =  iReply->header(QNetworkRequest::ContentLengthHeader);//there is a invalid value
      
      int ContentLength =  Variant2.toInt();
      
      qDebug()<< "ContentLengthHeader:"  + Variant2.toString();
      

      }@

      so please help me ,thanks

      1 Reply Last reply Reply Quote 0
      • raven-worx
        raven-worx Moderators last edited by

        Hi,
        if you only need to show a progress bar you can also use the signals "downloadProgress":http://qt-project.org/doc/qt-4.8/qnetworkreply.html#downloadProgress or "uploadProgress":http://qt-project.org/doc/qt-4.8/qnetworkreply.html#uploadProgress of your "QNetworkReply":qt-project.org/doc/qt-4.8/qnetworkreply.html

        --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
        If you have a question please use the forum so others can benefit from the solution in the future

        1 Reply Last reply Reply Quote 0
        • Z
          zhangjunhua last edited by

          he,but ,you must know the signale
          @"void downloadProgress(qint64 bytesReceived, qint64 bytesTotal);"@

          the bytesTotal is always -1 except at last the bytesReceived == bytesTotal.

          so while you are downloading something with post request, you do not know the bytesTotal,so how can you show a progressbar,you must know the bytesTotal in the headers with ContentLengthHeader,
          but i can not get it with the API
          @"QVariant Variant2 = iReply->header(QNetworkRequest::ContentLengthHeader);"@

          so it is not solutinon.

          1 Reply Last reply Reply Quote 0
          • raven-worx
            raven-worx Moderators last edited by

            i think connecting to the metaDataChanged() signal is wrong anyway.
            Try to use the signals suggested by me and gather the content length from the reply object in there.
            You can try
            @iReply->header(QNetworkRequest::ContentLengthHeader);@
            or
            @iReply->rawHeader ("ContentLengthHeader");@

            And note that the ContentLengthHeader isn't 100% accurate for progress indication, since there may be a request overhead, or requests may be compressed, etc.

            --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
            If you have a question please use the forum so others can benefit from the solution in the future

            1 Reply Last reply Reply Quote 0
            • Z
              zhangjunhua last edited by

              i have try it ,but i can not also get it .

              1 Reply Last reply Reply Quote 0
              • raven-worx
                raven-worx Moderators last edited by

                [quote author="zhangjunhua" date="1366101553"]i have try it ,but i can not also get it .
                [/quote]

                did you connect to the QNetworkReply::finished() signal and checked in the connected slot?!

                --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                If you have a question please use the forum so others can benefit from the solution in the future

                1 Reply Last reply Reply Quote 0
                • Z
                  zhangjunhua last edited by

                  yes ,i have connect to the QNetworkReply::finished() signal .-

                  1 Reply Last reply Reply Quote 0
                  • First post
                    Last post