Results 1 to 10 of 10

Thread: QHttp Response content

  1. #1
    Join Date
    Feb 2006
    Posts
    91
    Thanks
    4
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default QHttp Response content

    I am trying to login to a server and automate some manual stuff i.e. data entry and uploading files. Here is what I've encountered so far, I have successfully logged in and used QHttp:: post() to do the job. Now I am trying to upload some files, and I think I've got the multi-part file upload correct, however I don't know how to read the "response content" from the post. All I can read is the response header.

    I am using HttpFox to see the post messages, when I post a file through the browser, after the post-data is sent there is "response content" which is in HTML, how do I capture that ?
    Humans make mistake because there is really NO patch for HUMAN STUPIDITY

  2. #2
    Join Date
    Feb 2006
    Posts
    91
    Thanks
    4
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QHttp Response content

    I would like to clarify a bit more..there is a tab in HttpFox (Firefox Add-in) that is called "Content" tab. All I want to do is capture the content that would appear in that tab everytime I do a POST, but of course through QHttp. Is it possible ?
    Humans make mistake because there is really NO patch for HUMAN STUPIDITY

  3. #3
    Join Date
    Feb 2006
    Posts
    91
    Thanks
    4
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QHttp Response content

    aggghh...dumb me...it's just the body of the page that is returned after the request. I must be doing something wrong in the multi-part upload. Moderators may delete this post.
    Humans make mistake because there is really NO patch for HUMAN STUPIDITY

  4. #4
    Join Date
    Feb 2006
    Posts
    91
    Thanks
    4
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QHttp Response content

    Ok, I've been banging my head whole night but I still can't get it right. Here is the scenario:

    1. I am trying to upload a file through QHttp:: post()
    2. I think I am successful since I get the 200 OK response but I don't get to the correct page after uploading, why is this ? Am I doing something different than the browser ? Please help to identify

    here is the snippet of code I'm using


    Qt Code:
    1. ...
    2.  
    3. public:
    4. QByteArray p_flie;
    5. ....
    6.  
    7. QFile *fptr = new QFile(filePath);
    8. fptr->open(QIODevice::ReadOnly);
    9. QByteArray bytes = fptr->readAll();
    10.  
    11. this->p_file.append(QString("-----------------------------139712626415117\r\n").toUtf8());
    12. this->p_file.append(QString("Content-Disposition: form-data; name=\"workspace_item_id\"\r\n\r\n").toUtf8());
    13. this->p_file.append(QString(this->working_id + "\r\n").toUtf8());
    14. this->p_file.append(QString("-----------------------------139712626415117\r\n").toUtf8());
    15. this->p_file.append(QString("Content-Disposition: form-data; name=\"step\"\r\n\r\n").toUtf8());
    16. this->p_file.append(QString("3\r\n").toUtf8());
    17. this->p_file.append(QString("-----------------------------139712626415117\r\n").toUtf8());
    18. this->p_file.append(QString("Content-Disposition: form-data; name=\"page\"\r\n\r\n").toUtf8());
    19. this->p_file.append(QString("1\r\n").toUtf8());
    20. this->p_file.append(QString("-----------------------------139712626415117\r\n").toUtf8());
    21. this->p_file.append(QString("Content-Disposition: form-data; name=\"jsp\"\r\n\r\n").toUtf8());
    22. this->p_file.append(QString("/submit/choose-file.jsp\r\n").toUtf8());
    23. this->p_file.append(QString("-----------------------------139712626415117\r\n").toUtf8());
    24. this->p_file.append(QString(QString("Content-Disposition: form-data; name=\"file\"; filename=\"") + f.fileName() + QString("\"\r\n")).toUtf8());
    25. this->p_file.append(QString("Content-Type: text/plain\r\n\r\n").toUtf8());
    26. this->p_file.append(bytes);
    27. this->p_file.append(QString("\r\n").toUtf8());
    28. this->p_file.append(QString("-----------------------------139712626415117\r\n").toUtf8());
    29. this->p_file.append(QString("Content-Disposition: form-data; name=\"workspace_item_id\"\r\n\r\n").toUtf8());
    30. this->p_file.append(QString(this->working_id + "\r\n").toUtf8());
    31. this->p_file.append(QString("-----------------------------139712626415117\r\n").toUtf8());
    32. this->p_file.append(QString("Content-Disposition: form-data; name=\"step\"\r\n\r\n").toUtf8());
    33. this->p_file.append(QString("3\r\n").toUtf8());
    34. this->p_file.append(QString("-----------------------------139712626415117\r\n").toUtf8());
    35. this->p_file.append(QString("Content-Disposition: form-data; name=\"page\"\r\n\r\n").toUtf8());
    36. this->p_file.append(QString("1\r\n").toUtf8());
    37. this->p_file.append(QString("-----------------------------139712626415117\r\n").toUtf8());
    38. this->p_file.append(QString("Content-Disposition: form-data; name=\"jsp\"\r\n\r\n").toUtf8());
    39. this->p_file.append(QString("/submit/choose-file.jsp\r\n").toUtf8());
    40. this->p_file.append(QString("-----------------------------139712626415117\r\n").toUtf8());
    41. this->p_file.append(QString("Content-Disposition: form-data; name=\"submit_upload\"\r\n\r\n").toUtf8());
    42. this->p_file.append(QString("Next >\r\n").toUtf8());
    43. this->p_file.append(QString("-----------------------------139712626415117--\r\n").toUtf8());
    44.  
    45. httpPostFile("path to submit",this->p_file);
    46.  
    47. ...
    48. void AutoPost::httpPostFile(QString path,QByteArray postMsg)
    49. {
    50. QHttpRequestHeader header("POST",path);
    51. header.setContentType("multipart/form-data; boundary=-----------------------------139712626415117");
    52. header.setValue("Host", "localhost:8080");
    53. header.setValue("User-Agent","Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.3) Gecko/20090824 Firefox/3.5.3");
    54. header.setValue("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
    55. header.setValue("Accept-Language","en-us,en;q=0.5");
    56. header.setValue("Accept-Encoding","gzip,deflate");
    57. header.setValue("Accept-Charset","ISO-8859-1,utf-8;q=0.7,*;q=0.7");
    58. header.setValue("Keep-Alive","300");
    59. header.setValue("Connection","keep-alive");
    60. header.setValue("Cookie", cookie);
    61. header.setValue("Referer","http://localhost:8080/jspui/submit");
    62. header.setContentLength(postMsg.size());
    63.  
    64.  
    65. httpPostID = http->request(header,postMsg);
    66.  
    67.  
    68. }
    69.  
    70. ...
    71. //and I am reading the response like this
    72. connect(http, SIGNAL(requestFinished(int, bool)),
    73. this, SLOT(httpRequestFinished(int, bool)));
    74. ...
    75. void AutoPost::httpRequestFinished(int requestID, bool error)
    76. {
    77. if(httpPostID == requestID)
    78. {
    79. QByteArray byte = http->readAll();
    80. }
    81. }
    To copy to clipboard, switch view to plain text mode 

    Is there something missing in the way file upload works ? if I get a 200 OK doesn't that mean everything should be fine ? In what ways could this way be different from how the browser is doing the thing ? I've captured all the headers and I couldn't figure it out.
    Humans make mistake because there is really NO patch for HUMAN STUPIDITY

  5. #5
    Join Date
    Feb 2006
    Posts
    91
    Thanks
    4
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QHttp Response content

    Here are the headers from browser and my application

    Browser packets
    Header:

    POST /jspui/submit HTTP/1.1
    Host localhost:8080
    User-Agent Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.4) Gecko/20091016 Firefox/3.5.4
    Accept text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
    Accept-Language en-us,en;q=0.5
    Accept-Encoding gzip,deflate
    Accept-Charset ISO-8859-1,utf-8;q=0.7,*;q=0.7
    Keep-Alive 300
    Connection keep-alive
    Referer http://localhost:8080/jspui/submit
    Cookie JSESSIONID=696FF5F9F3FFE011DCD4662AEAC0BCF8
    Content-Type multipart/form-data; boundary=---------------------------210982431416320
    Content-Length 1161


    Post-Data

    -----------------------------210982431416320
    Content-Disposition: form-data; name="workspace_item_id"

    193
    -----------------------------210982431416320
    Content-Disposition: form-data; name="step"

    3
    -----------------------------210982431416320
    Content-Disposition: form-data; name="page"

    1
    -----------------------------210982431416320
    Content-Disposition: form-data; name="jsp"

    /submit/choose-file.jsp
    -----------------------------210982431416320
    Content-Disposition: form-data; name="file"; filename="essay.txt"
    Content-Type: text/plain

    This is a test file.
    -----------------------------210982431416320
    Content-Disposition: form-data; name="workspace_item_id"

    193
    -----------------------------210982431416320
    Content-Disposition: form-data; name="step"

    3
    -----------------------------210982431416320
    Content-Disposition: form-data; name="page"

    1
    -----------------------------210982431416320
    Content-Disposition: form-data; name="jsp"

    /submit/choose-file.jsp
    -----------------------------210982431416320
    Content-Disposition: form-data; name="submit_upload"

    Next >
    -----------------------------210982431416320--
    My Application
    Header

    POST /jspui/submit HTTP/1.1
    Host: localhost:8080
    User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.3) Gecko/20090824 Firefox/3.5.3
    Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
    Accept-Language: en-us,en;q=0.5
    Accept-Encoding: gzip,deflate
    Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
    Keep-Alive: 300
    Connection: keep-alive
    Referer: http://localhost:8080/jspui/submit
    Cookie: JSESSIONID=07B22972405E63E712CC957FEDC28647
    content-type: multipart/form-data; boundary=--AaB03x
    content-length: 765

    Post data

    --AaB03x
    Content-Disposition: form-data; name="workspace_item_id"

    194
    --AaB03x
    Content-Disposition: form-data; name="step"

    3
    --AaB03x
    Content-Disposition: form-data; name="page"

    1
    --AaB03x
    Content-Disposition: form-data; name="jsp"

    /submit/choose-file.jsp
    --AaB03x
    Content-Disposition: form-data; name="file"; filename="essay.txt"
    Content-Type: text/plain

    This is a test file.
    --AaB03x
    Content-Disposition: form-data; name="workspace_item_id"

    194
    --AaB03x
    Content-Disposition: form-data; name="step"

    3
    --AaB03x
    Content-Disposition: form-data; name="page"

    1
    --AaB03x
    Content-Disposition: form-data; name="jsp"

    /submit/choose-file.jsp
    --AaB03x
    Content-Disposition: form-data; name="submit_upload"

    Next >
    --AaB03x--

    Humans make mistake because there is really NO patch for HUMAN STUPIDITY

  6. #6
    Join Date
    Feb 2006
    Posts
    91
    Thanks
    4
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QHttp Response content

    Ok, after banging my head for another day, I still can't figure out if I am doing anything wrong. I realized that I am using Qt 4.5.2 so it could very well be a bug.

    In Qt 4.5.3 change log I see this

    QNetworkAccessManager
    * [256240] Proper handling of HTTP redirect in AlwaysCache mode.
    * [257662] Fix timing issues that could trigger double sending of an
    HTTP request.
    Please suggest if these bugs were related to QHttp too ? Should I use QNetworkAccessManager instead with the latest version 4.5.3 ?
    Humans make mistake because there is really NO patch for HUMAN STUPIDITY

  7. #7
    Join Date
    Oct 2009
    Posts
    37
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Re: QHttp Response content

    Quote Originally Posted by ct View Post
    Please suggest if these bugs were related to QHttp too ?
    they were not.

    Quote Originally Posted by ct View Post
    Should I use QNetworkAccessManager instead with the latest version 4.5.3 ?
    yes QHttp is deprecated with the soon to be released Qt 4.6.
    Disclaimer: Although I work on Qt for Nokia, anything I post here is personal

  8. #8
    Join Date
    Feb 2006
    Posts
    91
    Thanks
    4
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QHttp Response content

    I used curl and now everything is fine. This is how curl sent it's header

    Curl trace file:

    => Send header, 322 bytes (0x142)
    0000: POST /jspui/submit HTTP/1.1
    001d: User-Agent: curl/7.19.5 (i586-pc-mingw32msvc) libcurl/7.19.5 zli
    005d: b/1.2.3
    0066: Host: localhost:8080
    007c: Accept: */*
    0089: Cookie: JSESSIONID=C03B80796C0718E4CB5CA174422DD9FB
    00be: Content-Length: 1139
    00d4: Expect: 100-continue
    00ea: Content-Type: multipart/form-data; boundary=--------------------
    012a: --------13c11370a722
    0140:
    <= Recv header, 23 bytes (0x17)
    0000: HTTP/1.1 100 Continue
    => Send data, 551 bytes (0x227)
    0000: ------------------------------13c11370a722
    002c: Content-Disposition: form-data; name="workspace_item_id"
    0066:
    0068: 421
    006d: ------------------------------13c11370a722
    0099: Content-Disposition: form-data; name="step"
    00c6:
    00c8: 3
    00cb: ------------------------------13c11370a722
    00f7: Content-Disposition: form-data; name="page"
    0124:
    0126: 1
    0129: ------------------------------13c11370a722
    0155: Content-Disposition: form-data; name="jsp"
    0181:
    0183: /submit/choose-file.jsp
    019c: ------------------------------13c11370a722
    01c8: Content-Disposition: form-data; name="file"; filename="essay.txt
    0208: "
    020b: Content-Type: text/plain
    0225:
    => Send data, 20 bytes (0x14)
    0000: This is a test file.
    => Send data, 568 bytes (0x238)
    0000:
    0002: ------------------------------13c11370a722
    002e: Content-Disposition: form-data; name="workspace_item_id"
    0068:
    006a: 421
    006f: ------------------------------13c11370a722
    009b: Content-Disposition: form-data; name="step"
    00c8:
    00ca: 3
    00cd: ------------------------------13c11370a722
    00f9: Content-Disposition: form-data; name="page"
    0126:
    0128: 1
    012b: ------------------------------13c11370a722
    0157: Content-Disposition: form-data; name="jsp"
    0183:
    0185: /submit/choose-file.jsp
    019e: ------------------------------13c11370a722
    01ca: Content-Disposition: form-data; name="submit_upload"
    0200:
    0202: Next >
    020a: ------------------------------13c11370a722--
    <= Recv header, 17 bytes (0x11)
    0000: HTTP/1.1 200 OK
    <= Recv header, 27 bytes (0x1b)
    0000: Server: Apache-Coyote/1.1
    <= Recv header, 40 bytes (0x28)
    0000: expires: Thu, 01 Jan 1970 00:00:00 GMT
    <= Recv header, 18 bytes (0x12)
    0000: Pragma: no-cache
    <= Recv header, 25 bytes (0x19)
    0000: Cache-control: no-store
    <= Recv header, 39 bytes (0x27)
    0000: Content-Type: text/html;charset=UTF-8
    <= Recv header, 22 bytes (0x16)
    0000: Content-Language: en
    <= Recv header, 28 bytes (0x1c)
    0000: Transfer-Encoding: chunked
    <= Recv header, 37 bytes (0x25)
    0000: Date: Tue, 03 Nov 2009 16:41:43 GMT
    <= Recv header, 2 bytes (0x2)
    0000:
    For QHttp too I did receive 200 OK response..but the file was not uploaded. What is it that curl is doing different so that the file gets uploaded ?
    Humans make mistake because there is really NO patch for HUMAN STUPIDITY

  9. #9
    Join Date
    Feb 2006
    Posts
    91
    Thanks
    4
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QHttp Response content

    The only difference I see is Expect: , could it be this ?
    Humans make mistake because there is really NO patch for HUMAN STUPIDITY

  10. #10
    Join Date
    Mar 2010
    Location
    China
    Posts
    10
    Thanks
    2
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: QHttp Response content

    After refering your code, I made some modifies, i noticed that the boundary must encode with ASCII, it works for me now....i am still wondering when i will need to delete the reply2(QNetworkReply *reply2)....i am not sure if it cause memory leaking or not....
    sorry for my poor English, I'm a Chinese.....

    Qt Code:
    1. #include <QtGui>
    2. #include <QtNetwork/QNetworkAccessManager>
    3. #include <QUrl>
    4. #include <QtNetwork/QNetworkRequest>
    5. #include <QtNetwork/QNetworkReply>
    6.  
    7. static QNetworkReply *reply;
    8.  
    9. class TOOTLabel : public QLabel
    10. {
    11. Q_OBJECT
    12. public:
    13. TOOTLabel(QWidget *parent =0) : QLabel(parent) {
    14. uploadBtn = new QPushButton("uplaod....");
    15. connect(uploadBtn, SIGNAL(clicked()), this, SLOT(uploadFile()));
    16.  
    17. uploadBtn->show();
    18. uploadBtn->resize(240,100);
    19. }
    20.  
    21. ~TOOTLabel()
    22. {
    23. delete reply2;
    24. }
    25.  
    26. public slots:
    27. void TOOTReadyRead()
    28. {
    29. QImage img ;
    30. img.load(reply, "JPEG");
    31. setPixmap(QPixmap::fromImage(img));
    32. show();
    33. resize(img.width(),img.height());
    34. }
    35.  
    36. void uploadFile()
    37. {
    38. QStringList fileList = QFileDialog::getOpenFileNames(
    39. this,tr("Select some files to upload....")
    40. ,QDir::currentPath());
    41.  
    42. if(fileList.count() < 1) return;
    43.  
    44. manager2 = new QNetworkAccessManager;
    45. // QString fileName = "tootzoe.jpg";
    46. QString newPathName = "newtootzoe.jpg";
    47.  
    48. // foreach(QString fileName, fileList){
    49. QString fileName = fileList.at(0);
    50. file = new QFile(fileName);
    51. if(!file->open(QFile::ReadOnly )) { qDebug() << "Error opening file....";return ;}
    52.  
    53. ////===================================================
    54. QByteArray formData;
    55. QString _boundary = "-----------------------------7d6bb34502ce";
    56. QString contenttype = "multipart/form-data; boundary=" + _boundary;
    57.  
    58. formData.append(QString("--" + _boundary + "\r\n").toAscii());
    59. //
    60. formData.append(QString("Content-Disposition: form-data; name=\"Filename\"\r\n\r\n").toUtf8());
    61. formData.append(QString(fileName +"\r\n").toUtf8());
    62. formData.append(QString("--" + _boundary + "\r\n").toAscii()); // ASCII encoder
    63. //////////
    64. formData.append(QString("Content-Disposition: form-data; name=\"RelPathName\"\r\n\r\n").toUtf8());
    65. formData.append(QString(newPathName +"\r\n").toUtf8());
    66. formData.append(QString("--" + _boundary + "\r\n").toAscii()); // ASCII encoder
    67. //////////
    68. formData.append(QString("Content-Disposition: form-data; name=\"sqlQuery\"\r\n\r\n").toUtf8());
    69. formData.append(QString("FlashSentFileData\r\n").toUtf8());
    70. formData.append(QString("--" + _boundary + "\r\n").toAscii()); // ASCII encoder
    71. //数据块 begin
    72. formData.append(QString("Content-Disposition: form-data; name=\"Filedata\"; filename=\"" + fileName + "\"\r\n").toUtf8());
    73. formData.append(QString("Content-Type: application/octet-stream\r\n\r\n").toUtf8());
    74. formData.append( file->readAll());
    75. formData.append(QString("\r\n").toAscii());
    76. //数据块 end
    77. formData.append(QString("--" + _boundary + "--\r\n").toAscii());
    78.  
    79.  
    80. QNetworkRequest request2(QUrl("http://localhost/console/uploadfile.aspx")) ;
    81. request2.setHeader(QNetworkRequest::ContentTypeHeader, contenttype);
    82. request2.setHeader(QNetworkRequest::ContentLengthHeader, formData.size());
    83. reply2 = manager2->post(request2, formData);
    84. connect(reply2, SIGNAL(finished()), this,SLOT(uploadFileFinished()));
    85. qDebug() << "uploading " << fileName;
    86.  
    87. // }
    88. file->close();
    89.  
    90. }
    91.  
    92. void uploadFileFinished()
    93. {
    94. qDebug() << "finished upload.... " << reply2->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() ;
    95. qDebug() << QString( reply2->readAll() );
    96. }
    97.  
    98.  
    99. private:
    100. QPushButton *uploadBtn;
    101. QNetworkAccessManager *manager2;
    102. QNetworkReply *reply2;
    103. QFile *file;
    104.  
    105. };
    106.  
    107. int main(int argc, char **argv)
    108. {
    109. QApplication app(argc, argv);
    110.  
    111. QNetworkAccessManager manager;
    112. QNetworkRequest request;
    113. request.setUrl(QUrl("http://www.google.com/intl/en_ALL/images/logo2.gif"));
    114.  
    115. reply = manager.get(request);
    116. TOOTLabel imgLabel;
    117. QObject::connect( reply, SIGNAL(finished()), &imgLabel, SLOT(TOOTReadyRead()));
    118.  
    119. imgLabel.show();
    120.  
    121. return app.exec();
    122. }
    123.  
    124. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. QHTTP does not see tunneled TCP HTTP OK authentication response
    By SailingDreams in forum Qt Programming
    Replies: 6
    Last Post: 23rd May 2009, 09:39
  2. From QHttp to QHttp over SSL
    By Nyphel in forum Newbie
    Replies: 1
    Last Post: 3rd July 2007, 10:41
  3. QHttp with localhost.
    By William Wilson in forum Qt Programming
    Replies: 3
    Last Post: 30th June 2007, 20:26
  4. how to use QHttp inside QThread in Qt3
    By alusuel in forum Qt Programming
    Replies: 3
    Last Post: 14th July 2006, 11:19
  5. Help with QHttp needed.
    By bitChanger in forum Qt Programming
    Replies: 5
    Last Post: 25th January 2006, 21:09

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.