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. Can not get the header(QNetworkRequest::ContentLengthHeader) with a post request
QtWS25 Last Chance

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

Scheduled Pinned Locked Moved General and Desktop
7 Posts 2 Posters 4.7k 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.
  • Z Offline
    Z Offline
    zhangjunhua
    wrote on last edited by
    #1

    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
    0
    • raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      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
      0
      • Z Offline
        Z Offline
        zhangjunhua
        wrote on last edited by
        #3

        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
        0
        • raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by
          #4

          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
          0
          • Z Offline
            Z Offline
            zhangjunhua
            wrote on last edited by
            #5

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

            1 Reply Last reply
            0
            • raven-worxR Offline
              raven-worxR Offline
              raven-worx
              Moderators
              wrote on last edited by
              #6

              [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
              0
              • Z Offline
                Z Offline
                zhangjunhua
                wrote on last edited by
                #7

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

                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