Page 1 of 2 12 LastLast
Results 1 to 20 of 22

Thread: HttpHeader Request only part of an entity

  1. #1
    Join Date
    Mar 2010
    Posts
    92
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Exclamation HttpHeader Request only part of an entity

    Hi dear all,

    I'm reading the code of QT attached http sample(examples\network\http).

    In HttpWindow::downloadFile(), I added a key&value pair to the
    QHttpRequestHeader, like this:



    Qt Code:
    1. void HttpWindow::downloadFile()
    2. {
    3. QString qsLink = urlLineEdit->text();
    4. QUrl quUrl(qsLink);
    5. QString qsUrlPath = quUrl.path();
    6. QFileInfo qfiFileInfor(qsUrlPath);
    7. QString qsFileName = qfiFileInfor.fileName();
    8. if (qsFileName.isEmpty())
    9. qsFileName = "index.html";
    10.  
    11. if (QFile::exists(qsFileName)) {
    12. if (QMessageBox::question(this, tr("HTTP"), tr("There already exists a file called %1 in the current directory. Overwrite?").arg(qsFileName), QMessageBox::Yes|QMessageBox::No, QMessageBox::No)== QMessageBox::No){
    13. return;
    14. }
    15. QFile::remove(qsFileName);
    16. }
    17. qfFileOut = new QFile(qsFileName);
    18. if (!qfFileOut->open(QIODevice::WriteOnly)) {
    19. QMessageBox::information(this, tr("HTTP"),tr("Unable to save the file %1: %2.").arg(qsFileName).arg(qfFileOut->errorString()));
    20. delete qfFileOut;
    21. qfFileOut = 0;
    22. return;
    23. }
    24. QString qsHost = quUrl.host();
    25. QHttp::ConnectionMode qhconMode = quUrl.scheme().toLower() == "https" ? QHttp::ConnectionModeHttps : QHttp::ConnectionModeHttp;
    26. qhHttp->setHost(qsHost, qhconMode, quUrl.port() == -1 ? 0 : quUrl.port());
    27. if (!quUrl.userName().isEmpty())
    28. qhHttp->setUser(quUrl.userName(), quUrl.password());
    29. bHttpRequestAborted = false;
    30. QByteArray qbaUrlPath = QUrl::toPercentEncoding(qsUrlPath, "!$&'()*+,;=:@/");
    31. if (qbaUrlPath.isEmpty())
    32. qbaUrlPath = "/";//_________________________________________
    33. QHttpRequestHeader *qhrhHeader = new QHttpRequestHeader("GET", qbaUrlPath);
    34. qhrhHeader->setValue("Connection", "Keep-Alive");
    35. qhrhHeader->setValue("Accept-Ranges", "byte");
    36. /*qhrhHeader->setValue("Content-Length", "1024");
    37. qhrhHeader->setValue("Content-Type", "application/x-rar-compressed");*/
    38. qhrhHeader->setValue("User-Agent", "Mozilla/5.0");
    39. qhrhHeader->setValue("Range", "bytes=0-999");
    40. qhrhHeader->setValue("Host", qsHost);
    41. iHttpGetId = qhHttp->request(*qhrhHeader, 0, qfFileOut);//_________________________________
    42. qpProgressDialog->setWindowTitle(tr("HTTP"));
    43. qpProgressDialog->setLabelText(tr("Downloading %1.").arg(qsFileName));
    44. downloadButton->setEnabled(false);
    45. }
    To copy to clipboard, switch view to plain text mode 


    but when i build it return on funcition is 206


    10.2.7 206 Partial Content

    The server has fulfilled the partial GET request for the resource. The request MUST have included a Range header field (section 14.35) indicating the desired range, and MAY have included an If-Range header field (section 14.27) to make the request conditional.

    The response MUST include the following header fields:

    - Either a Content-Range header field (section 14.16) indicating
    the range included with this response, or a multipart/byteranges
    Content-Type including Content-Range fields for each part. If a
    Content-Length header field is present in the response, its
    value MUST match the actual number of OCTETs transmitted in the
    message-body.

    - Date

    - ETag and/or Content-Location, if the header would have been sent
    in a 200 response to the same request

    - Expires, Cache-Control, and/or Vary, if the field-value might
    differ from that sent in any previous response for the same
    variant

    If the 206 response is the result of an If-Range request that used a strong cache validator (see section 13.3.3), the response SHOULD NOT include other entity-headers. If the response is the result of an If-Range request that used a weak validator, the response MUST NOT include other entity-headers; this prevents inconsistencies between cached entity-bodies and updated headers. Otherwise, the response MUST include all of the entity-headers that would have been returned with a 200 (OK) response to the same request.

    A cache MUST NOT combine a 206 response with other previously cached content if the ETag or Last-Modified headers do not match exactly, see 13.5.4.

    A cache that does not support the Range and Content-Range headers MUST NOT cache 206 (Partial) responses.

    can you help me and edit my source

    http://www.mediafire.com/?dzjaztigrnm
    Last edited by wysota; 22nd April 2010 at 10:52. Reason: Removed inline image

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: HttpHeader Request only part of an entity

    So what's wrong with getting 206? That's not an error... It's just QHttp (which is obsolete since a few minor releases of Qt) doesn't support it.

  3. #3
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: HttpHeader Request only part of an entity

    "Content-Length" and "Content-type" is server header, not client header.

  4. #4
    Join Date
    Mar 2010
    Posts
    92
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Default Re: HttpHeader Request only part of an entity

    Quote Originally Posted by fatjuicymole View Post
    "Content-Length" and "Content-type" is server header, not client header.
    you can see link Http Header : http://en.wikipedia.org/wiki/List_of_HTTP_headers

    So what's wrong with getting 206? That's not an error... It's just QHttp (which is obsolete since a few minor releases of Qt) doesn't support it.


    i dont think so, i download file DownloadFile.cpp on link: http://cpowone.com/DownloadFile.cpp
    error but when i download html,Ex: http://www.qtcentre.org/forums/2-Qt-Programming ,result download good,i think i use confused Header Http
    Last edited by wysota; 22nd April 2010 at 10:51. Reason: Removed inline image

  5. #5
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: HttpHeader Request only part of an entity

    Quote Originally Posted by nhs_0702 View Post
    Yes, but your request header is neither a constant 1024 bytes, nor rar-compressed, so both are invalid. So what you are sending to the server is invalid.

  6. #6
    Join Date
    Mar 2010
    Posts
    92
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Default Re: HttpHeader Request only part of an entity

    Quote Originally Posted by fatjuicymole View Post
    Yes, but your request header is neither a constant 1024 bytes, nor rar-compressed, so both are invalid. So what you are sending to the server is invalid.
    yes,beacause i dont known get Content-Type and Content-Length ,but when i download file http://cpowone.com/DownloadFile.cpp have size Content-Length = 24,487 bytes and Content-Type = text/plain --> it dont download,i dont understand


    My new Project :
    http://www.4shared.com/file/y0yjaia8/http_224.html

    Qt Code:
    1. void HttpWindow::downloadFile()
    2. {
    3. QString qsLink = urlLineEdit->text();
    4. QUrl quUrl(qsLink);
    5. QString qsUrlPath = quUrl.path();
    6. QFileInfo qfiFileInfor(qsUrlPath);
    7. QString qsFileName = qfiFileInfor.fileName();
    8. if (qsFileName.isEmpty())
    9. qsFileName = "index.html";
    10.  
    11. if (QFile::exists(qsFileName)) {
    12. if (QMessageBox::question(this, tr("HTTP"), tr("There already exists a file called %1 in the current directory. Overwrite?").arg(qsFileName), QMessageBox::Yes|QMessageBox::No, QMessageBox::No)== QMessageBox::No){
    13. return;
    14. }
    15. QFile::remove(qsFileName);
    16. }
    17. qfFileOut = new QFile(qsFileName);
    18. if (!qfFileOut->open(QIODevice::WriteOnly)) {
    19. QMessageBox::information(this, tr("HTTP"),tr("Unable to save the file %1: %2.").arg(qsFileName).arg(qfFileOut->errorString()));
    20. delete qfFileOut;
    21. qfFileOut = 0;
    22. return;
    23. }
    24. QString qsHost = quUrl.host();
    25. QHttp::ConnectionMode qhconMode = quUrl.scheme().toLower() == "https" ? QHttp::ConnectionModeHttps : QHttp::ConnectionModeHttp;
    26. qhHttp->setHost(qsHost, qhconMode, quUrl.port() == -1 ? 0 : quUrl.port());
    27. if (!quUrl.userName().isEmpty())
    28. qhHttp->setUser(quUrl.userName(), quUrl.password());
    29. bHttpRequestAborted = false;
    30. QByteArray qbaUrlPath = QUrl::toPercentEncoding(qsUrlPath, "!$&'()*+,;=:@/");
    31. if (qbaUrlPath.isEmpty())
    32. qbaUrlPath = "/";//_________________________________________
    33. QHttpRequestHeader *qhrhHeader = new QHttpRequestHeader("GET", qbaUrlPath);
    34. qhrhHeader->setValue("Connection", "Keep-Alive");
    35. qhrhHeader->setValue("Accept-Ranges", "byte");
    36. qhrhHeader->setValue("Content-Length", "24487");
    37. qhrhHeader->setValue("Content-Type", "text/plain");
    38. qhrhHeader->setValue("Date", "Tue, 15 Nov 1994 08:12:31 GMT");// Date: Tue, 15 Nov 1994 08:12:31 GMT
    39. qhrhHeader->setValue("Content-Location", "/index.htm");// :
    40. qhrhHeader->setValue("Expires", "Thu, 01 Dec 1994 16:00:00 GMT");
    41. qhrhHeader->setValue("User-Agent", "Mozilla/5.0");//Expires: Thu, 01 Dec 1994 16:00:00 GMT
    42. qhrhHeader->setValue("Range", "bytes=0-999");
    43. qhrhHeader->setValue("Host", qsHost);
    44. iHttpGetId = qhHttp->request(*qhrhHeader, 0, qfFileOut);//_________________________________
    45. qpProgressDialog->setWindowTitle(tr("HTTP"));
    46. qpProgressDialog->setLabelText(tr("Downloading %1.").arg(qsFileName));
    47. downloadButton->setEnabled(false);
    48. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 22nd April 2010 at 10:51. Reason: Removed inline image

  7. #7
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: HttpHeader Request only part of an entity

    You mis-understand. If you use headers such as Content-Length in your request header, you are specifying the length of your request body. When they are used by the server, they are specifying the length of the response body.

    If you wish to only request the first 1024 bytes of a document, use the "Range" header.

  8. #8
    Join Date
    Mar 2010
    Posts
    92
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Default Re: HttpHeader Request only part of an entity

    Quote Originally Posted by fatjuicymole View Post
    You mis-understand. If you use headers such as Content-Length in your request header, you are specifying the length of your request body. When they are used by the server, they are specifying the length of the response body.

    If you wish to only request the first 1024 bytes of a document, use the "Range" header.
    please read my code, because I still use the "range" http header, but no connection, it error 206, if so would you please edit my project, thanks

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: HttpHeader Request only part of an entity

    You also tell the server - "hey, there are 1024 bytes available for you to read after the last header" which is not true. I don't know if it confuses the server trying to read those 1024 bytes but I guess it could.

    Please, don't expect anyone to edit your project for you Unless of course you offer a decent pay but then you should be posting in the "jobs" section of the site.

  10. #10
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: HttpHeader Request only part of an entity

    Quote Originally Posted by nhs_0702 View Post
    please read my code, because I still use the "range" http header, but no connection, it error 206, if so would you please edit my project, thanks
    I very much doubt anyone here is going to edit your project. We are doing this for free, you should appreciate the helpful advice.

    Secondly, 206 isn't an error. It means success. You asked for partial content, and the server provided it.

  11. #11
    Join Date
    Mar 2010
    Posts
    92
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Default Re: HttpHeader Request only part of an entity

    Quote Originally Posted by wysota View Post
    You also tell the server - "hey, there are 1024 bytes available for you to read after the last header" which is not true. I don't know if it confuses the server trying to read those 1024 bytes but I guess it could.

    Please, don't expect anyone to edit your project for you Unless of course you offer a decent pay but then you should be posting in the "jobs" section of the site.
    If I do not have "length Content 'and' Content-Type" will always error 206, how about you explain this?

    Quote Originally Posted by fatjuicymole View Post
    I very much doubt anyone here is going to edit your project. We are doing this for free, you should appreciate the helpful advice.

    Secondly, 206 isn't an error. It means success. You asked for partial content, and the server provided it.
    My purpose is want to download each file, if the server returns a 206 error, which means I have not down anything, I'd love to do a similar thing as Internet Download Manager
    Last edited by nhs_0702; 22nd April 2010 at 19:28.

  12. #12
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: HttpHeader Request only part of an entity

    Quote Originally Posted by nhs_0702 View Post
    If I do not have "length Content 'and' Content-Type" will always error 206, how about you explain this?
    That would be correct, as those headers are nothing to do with how many bytes you are requesting, they are to tell the server how many bytes you are SENDING.

    Quote Originally Posted by nhs_0702 View Post
    My purpose is want to download each file, if the server returns a 206 error, which means I have not down anything
    Actually, 206 means the server is returning partial file content (rather than the full file).

  13. #13
    Join Date
    Mar 2010
    Posts
    92
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Default Re: HttpHeader Request only part of an entity

    Quote Originally Posted by fatjuicymole View Post
    That would be correct, as those headers are nothing to do with how many bytes you are requesting, they are to tell the server how many bytes you are SENDING.

    Actually, 206 means the server is returning partial file content (rather than the full file).
    yes,i thank you very much,I was successful
    if you allow, I want to ask you a bit more, you can only way I get "file size" of a file on the download link ?

  14. #14
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: HttpHeader Request only part of an entity

    Send a HEAD request to the server, it will reply with the same header (but no body) as if you sent GET. In this header, you will receive Content-length which will be the size of the file as long as you do not include a "Range" header in your HEAD request (else you'll get the size of your range, which isn't what you want).

    Note that not all servers support this, in which case you will be transferring "blind" and will have to depend on a short read.

  15. #15
    Join Date
    Mar 2010
    Posts
    92
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Default Re: HttpHeader Request only part of an entity

    Quote Originally Posted by fatjuicymole View Post
    Send a HEAD request to the server, it will reply with the same header (but no body) as if you sent GET. In this header, you will receive Content-length which will be the size of the file as long as you do not include a "Range" header in your HEAD request (else you'll get the size of your range, which isn't what you want).

    Note that not all servers support this, in which case you will be transferring "blind" and will have to depend on a short read.
    yes, I also think it will get by, but really I do not known write in QTcreator how to get respond Http header, I havent example by learn

  16. #16
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: HttpHeader Request only part of an entity

    Come on, man. Simply instead of a "get" request, make a "head" request.

  17. #17
    Join Date
    Mar 2010
    Posts
    92
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Default Re: HttpHeader Request only part of an entity

    Quote Originally Posted by wysota View Post
    Come on, man. Simply instead of a "get" request, make a "head" request.
    Qt Code:
    1. QFile qfFileHeader("Header.txt");
    2. if (!qfFileHeader.open(QIODevice::WriteOnly)) {
    3. QMessageBox::information(this, tr("HTTP"),tr("Unable to save the file %1: %2.").arg(qsFileName).arg(qfFileOut->errorString()));
    4. return;
    5. }
    6. QHttpRequestHeader qhrhHeaderRespond("HEAD", qbaUrlPath);
    7. qhrhHeaderRespond.setValue("Host", qsHost);
    8. qhHttpRespond->setHost(qsHost, qhconMode, quUrl.port() == -1 ? 0 : quUrl.port());
    9. if (!quUrl.userName().isEmpty())
    10. qhHttpRespond->setUser(quUrl.userName(), quUrl.password());
    11. qhHttpRespond->request(qhrhHeaderRespond, 0, &qfFileHeader);
    To copy to clipboard, switch view to plain text mode 

    it dont get in File Header.txt
    Last edited by nhs_0702; 23rd April 2010 at 16:11.

  18. #18
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: HttpHeader Request only part of an entity

    I think you misunderstand what a "head" request is.

    http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  19. #19
    Join Date
    Mar 2010
    Posts
    92
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Default Re: HttpHeader Request only part of an entity

    Quote Originally Posted by wysota View Post
    I think you misunderstand what a "head" request is.

    http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html
    must i get content-length in Funcition void HttpWindow::readResponseHeader(const QHttpResponseHeader &responseHeader) by funciton QString s =responseHeader.toString(); ?
    but i get mis information

    Ex:
    + s "HTTP/1.1 200 OK
    Date: Sat, 24 Apr 2010 00:41:00 GMT
    Server: Apache/2.0.63 (Unix) mod_ssl/2.0.63 OpenSSL/0.9.8e-fips-rhel5 mod_auth_passthrough/2.1 mod_bwlimited/1.4 PHP/5.2.8
    Last-Modified: Sat, QString

  20. #20
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: HttpHeader Request only part of an entity

    Quote Originally Posted by nhs_0702 View Post
    must i get content-length in Funcition void HttpWindow::readResponseHeader(const QHttpResponseHeader &responseHeader) by funciton QString s =responseHeader.toString(); ?
    but i get mis information
    I have no idea what you mean. Please rephrase the question.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. set visable part of view
    By ensky_cy in forum Qt Programming
    Replies: 0
    Last Post: 16th December 2009, 11:21
  2. Special character's HTML entity to string
    By Hiba in forum Qt Programming
    Replies: 4
    Last Post: 3rd March 2009, 15:05
  3. Qt Tutorial #1 Add-on Part II
    By ShaChris23 in forum Newbie
    Replies: 5
    Last Post: 25th April 2007, 20:00
  4. how to get a part of the string
    By probine in forum Newbie
    Replies: 2
    Last Post: 4th December 2006, 15:37
  5. Qhttp::request(...) method and GET request
    By mikhailt in forum Qt Programming
    Replies: 4
    Last Post: 15th September 2006, 13:26

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.