Results 1 to 3 of 3

Thread: Whats the problem with my code (http post)

  1. #1
    Join Date
    Oct 2010
    Posts
    16
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Red face Whats the problem with my code (http post)

    Hi..i am developing a simple application which uploads image to yfrog.com.(These images will be reflected in twitter account). Here is my code. but it is not working. I am not getting response from server.

    QNetworkAccessManager *manager = new QNetworkAccessManager(this);
    QNetworkRequest request(QUrl("http://yfrog.com/api/uploadAndPost"));

    QByteArray data;
    QUrl params,params1;
    QFile file("some image path");
    QString boundary("-----abcde12345");
    QString body = "\r\n--" + boundary + "\r\n";

    params.addQueryItem("username",twitterusername);
    params.addQueryItem("password",twitterpassword);
    params.addQueryItem("message",some message...);
    params.addQueryItem("key",mydeveloperkey);


    data.append(body);
    data.append(params.toString());
    QByteArray ba;
    ba=file.readAll();
    QString body1(ba);
    params1.addQueryItem("media",body1);
    data.append(params1.toString());
    data.append(body);


    request.setRawHeader("Content-Type","multipart/form-data; boundary=-----abcde12345");
    request.setHeader(QNetworkRequest::ContentLengthHe ader,data.size());

    QNetworkReply *reply = manager->post(request,data);

    reply->waitForReadyRead(-1);
    qDebug() << "replay :"<<reply->readAll();



    If i checked the requested TCP packets from wireshark, it is giving a error message like 'malformed packets'.

    For reference : http://code.google.com/p/imageshacka...GuploadAndPost


    Please any body help regarding this. Where i am doing wrong?

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

    Default Re: Whats the problem with my code (http post)

    So what is the difference between the packets sent by your application and the packets sent by a web browser?

    You can confirm this using wireshark, as you have used already.

    If you are getting an error, please post a screenshot of that error.

    Are you correctly mime encoding the image data to a format which the web site supports?

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Whats the problem with my code (http post)

    You are missing [code] tags around your code.

    The boundary value almost certainly needs to be quoted in the multipart/form-data content type.
    The params probably should be extracted with QUrl::encodedQuery() rather than toString().

    Do you really need multipart/form-data if there is only one part?

Similar Threads

  1. Replies: 3
    Last Post: 18th October 2010, 17:34
  2. HTTP POST method
    By Macok in forum Qt Programming
    Replies: 5
    Last Post: 25th July 2010, 14:33
  3. Http post
    By Max123 in forum Qt Programming
    Replies: 1
    Last Post: 30th May 2010, 13:43
  4. Http Post method help
    By ravirdv in forum Qt Programming
    Replies: 0
    Last Post: 27th September 2009, 12:15
  5. http POST with form encoding
    By srikanth_trulyit in forum Qt Programming
    Replies: 1
    Last Post: 10th June 2009, 10:16

Tags for this Thread

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
  •  
Qt is a trademark of The Qt Company.