Results 1 to 4 of 4

Thread: POST request to a web service

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #2
    Join Date
    Oct 2008
    Location
    Kharkov, Ukraine
    Posts
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanked 1 Time in 1 Post

    Default Re: POST request to a web service

    Quote Originally Posted by QPlace View Post
    I have a web service in ASP.Net, getManagers, that accepts one parameter, UserName and returns a list of strings to a client.

    I am trying to build the POST request from QT and I will appreciate any comments or suggestions on this subject

    Here is what I do:
    QHttpRequestHeader header("POST", "/StoreManager/getManagers.asmx");
    header.setValue("Host", "localhost:1048");
    header.setValue("charset", "utf-8");
    header.setContentType("application/soap+xml");

    m_http.setHost("http://localhost:1048");
    m_http.request(header, "UserName=QPlace");

    connect((QObject*)(&m_http), SIGNAL(requestFinished ( int id, bool error )), this, SLOT(ThisReqestIsCompleted ( int id, bool error )));


    First problem is that ThisReqestIsCompleted is never called. But I am unsure if I am doing the call to the service correctly. All comments and suggestions are greatly appreciated
    Query is incorrect in terms of HTTP proto. let's look at peace of code that sending file to server:
    Qt Code:
    1. QByteArray boundary = QByteArray::number(reinterpret_cast<long long>(this));
    2. QByteArray begin_data="--"+boundary+"\r\nContent-Disposition: form-data; name=\"";
    3. begin_data+=conf->get_config_for("FILE_INPUT_NAME");
    4. begin_data+="\"; filename=\"test.txt\"\r\n\r\n";
    5. QByteArray content_data;
    6. content_data=read->readAll();
    7. content_data+="\r\n";
    8. QByteArray passwd_data = "--"+boundary+"\r\nContent-Disposition: form-data; name=\"code\"\r\n\r\n";
    9. passwd_data+=conf->get_config_for("ACCESS_CODE");
    10. QByteArray end_data="\r\n--"+boundary+"--\r\n";
    11. QByteArray data = begin_data+content_data+passwd_data+end_data;
    12.  
    13. QHttpRequestHeader header("POST",conf->get_config_for("HOST_UPLOAD_PATH"));
    14. header.setValue("Host",conf->get_config_for("HOST_NAME"));
    15. header.setValue("Content-type","multipart/form-data; boundary="+boundary);
    16. header.setValue("Content-Transfer-Encoding","binary");
    17.  
    18. if(conf->get_config_for("PROXY_HOST").size()!=0) {
    19. http->setProxy(conf->get_config_for("PROXY_HOST"),
    20. conf->get_config_for("PROXY_PORT").toInt(),
    21. conf->get_config_for("PROXY_AUTH_USERNAME"),
    22. conf->get_config_for("PROXY_AUTH_PASSWORD"));
    23. }
    24.  
    25. http->setHost(conf->get_config_for("HOST_NAME"));
    26. current_request=http->request(header,data);
    27. req_finish.lock();
    To copy to clipboard, switch view to plain text mode 
    and finish signal handler:
    Qt Code:
    1. void http_daemon::request_signal(int r_numb,bool err) {
    2. log<<"Req fihish recvd:"<<r_numb<<" with err state:"<<err<<"\n";
    3. log<<QString(http->readAll())<<"\n";
    4.  
    5. if(current_request==r_numb) {
    6. // This req is waiting for req. Unlock main send thread!
    7. req_finish.unlock();
    8. }
    9. }
    To copy to clipboard, switch view to plain text mode 

    Mutex req_finish used to block send function(thread) (first pease of code) until req is finished. (or some data, in your case QHttp object, would been destroyed after function exit)

    PS
    From QHttp docs:
    The class works asynchronously, so there are no blocking functions. If an operation cannot be executed immediately, the function will still return straight away and the operation will be scheduled for later execution. The results of scheduled operations are reported via signals. This approach depends on the event loop being in operation.
    Last edited by defender; 5th November 2008 at 13:30.

Similar Threads

  1. Creating QDS service
    By bowser in forum Qt for Embedded and Mobile
    Replies: 1
    Last Post: 29th October 2007, 12:12
  2. Https POST Request
    By munna in forum Qt Programming
    Replies: 10
    Last Post: 11th November 2006, 15:24

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.