Results 1 to 5 of 5

Thread: QHttp::post() help

  1. #1
    Join Date
    Feb 2006
    Location
    Stockholm
    Posts
    5
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Unhappy QHttp::post() help

    I am having trouble with QHttp and related classes when trying to post a file to a server.
    No matter what I try, the header response is: "Internal server error"

    My code below (error checks, signals and checks for return values removed for clarity)
    Qt Code:
    1. QHttp *http;
    2. QFile *file;
    3. QUrl url(lineEdit_url->text());
    4. file = new QFile(lineEdit_filename->text());
    5. file->open(QIODevice::ReadOnly)
    6. http->setHost(url.host(),url.port() != -1 ? url.port() : 80);
    7. if(!url.userName().isEmpty())
    8. http->setUser(url.userName(),url.password());
    9. http->post(url.path(),file);
    To copy to clipboard, switch view to plain text mode 

    Here is a working html form for posting to this server:
    Qt Code:
    1. <form method=post enctype="multipart/form-data" action=/uploader/Default.asp>
    2. Username:<br/><input type="text" name="username"><br/><br/>
    3. Password:<br/><input type="password" name="password"><br/><br/>
    4. Your File:<BR><input type=file name=YourFile><BR><BR>
    5. <input type=submit name=submit value="Upload">
    6. </form>
    To copy to clipboard, switch view to plain text mode 

    There are no examples in qtassistant apart from the brief description of QHttp:: post()

    Any help is deeply appreciated!

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QHttp::post() help

    You can use Ethereal or Proxomitron to see how browsers post files and how your program does this. You will probably have to use QHttpRequestHeader to set proper content type.

  3. #3
    Join Date
    Feb 2006
    Posts
    9
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QHttp::post() help

    Qt Code:
    1. void HttpWindow::uploadFile()
    2. {
    3. QString fileName = QFileDialog::getOpenFileName(this,
    4. tr("OpenFile"),
    5. openFilesPath,
    6. tr("All Files (*);;Text Files (*.txt)"));
    7. if (!fileName.isEmpty())
    8. openFilesPath = fileName;
    9. QFileInfo path(openFilesPath);
    10. QString fileName1 = path.fileName();
    11. userfile = new QFile(openFilesPath);
    12. if ( !userfile->open(QIODevice::ReadOnly) )
    13. {
    14. QMessageBox::information(this, tr("HTTP"),
    15. tr("Unable to open the file %1: %2.")
    16. .arg(openFilesPath).arg(userfile->errorString()));
    17.  
    18. }
    19.  
    20. QHttpRequestHeader header("POST", "/upload.php", 1, 1);
    21. header.setValue("Host", "foliant");
    22. header.setValue("Content-type", "multipart/form-data, boundary=AaB03x");
    23. header.setValue("Cache-Control", "no-cache");
    24. header.setValue("Accept","*/*");
    25.  
    26. QByteArray byt(openFilesPath.toUtf8());
    27. QByteArray bytes;
    28. bytes.append("--AaB03x\r\n");
    29. bytes.append("content-disposition: ");
    30. bytes.append("form-data; name=\"agency\"\r\n");
    31. bytes.append("\r\n");
    32. bytes.append("0\r\n");
    33. bytes.append("--AaB03x\r\n");
    34. bytes.append("content-disposition: ");
    35. bytes.append("form-data; name=\"userfile\"; filename=\"" + byt+ "\"\r\n");
    36. bytes.append("Content-Transfer-Encoding: binary\r\n");
    37. bytes.append("\r\n");
    38. bytes.append(userfile->readAll());
    39. userfile->close(); // the file is opened earlier in the code
    40. bytes.append("\r\n");
    41. bytes.append("--AaB03x--");
    42. int contentLength = bytes.length();
    43. header.setContentLength(contentLength);
    44.  
    45. http->setHost("foliant");
    46. httpRequestAborted = false;
    47. httpGetId = http->request(header, bytes);
    48. }
    To copy to clipboard, switch view to plain text mode 

  4. The following user says thank you to crocus for this useful post:

    olidem (4th March 2010)

  5. #4
    Join Date
    Feb 2006
    Location
    Stockholm
    Posts
    5
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Smile Re: QHttp::post() help

    Thanks a lot guys, got it working!
    Seems there was a retarded VB script at the other end that's case-sensitive in places it shouldn't be...

  6. #5
    Join Date
    Mar 2006
    Posts
    21
    Qt products
    Qt3
    Platforms
    Unix/X11

    Lightbulb Re: QHttp::post() help

    hi guys am newbie , am also working on webbrowser i need ur help i want to know the location of these codes , or please mail me the http related code to me, my email is awalesminfo@gmail.com

Similar Threads

  1. Qhttp:Post() - multipart/form-data
    By arunredi in forum Newbie
    Replies: 5
    Last Post: 20th September 2011, 21:02
  2. Replies: 11
    Last Post: 20th January 2009, 15:10
  3. QHttp::post() - cannot upload file
    By arunredi in forum Qt Programming
    Replies: 5
    Last Post: 16th May 2008, 13:13

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.