Results 1 to 4 of 4

Thread: POST request to a web service

  1. #1
    Join Date
    Jul 2007
    Posts
    121
    Thanks
    38
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Angry POST request to a web service

    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

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

    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 12:30.

  3. #3
    Join Date
    Jul 2007
    Posts
    121
    Thanks
    38
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: POST request to a web service

    Thank you for your answer. I am not sure that I agree with it relative to my implementation. May be you can take a look at it (code below)

    class RemoteManagers : public QObject
    {
    Q_OBJECT
    private:
    QHttp m_http;
    private slots:
    void OnrequestFinished ( int id, bool error );
    public:
    void GetManagers();
    };


    class CMainView : public QMainWindow
    {
    Q_OBJECT
    private:
    RemoteManagers m_remotemanagers;
    private slots:
    void OnGetManagers();
    void OnManagersReceived (QStringList lst);
    };


    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    CMainView w;
    w.show();
    return a.exec();
    }


    void CMainView::OnGetManagers()
    {
    m_remotemanagers.GetManagers();
    }


    void RemoteManagers::GetManagers()
    {
    QHttpRequestHeader* myheader = new QHttpRequestHeader("POST", "/MyWebsite/GetManagers.asmx");
    QHttpRequestHeader& header = *myheader;
    header.setValue("Host", "http://localhost:1048");
    header.setValue("charset", "utf-8");
    header.setContentType("application/x-www-form-urlencoded");
    header.setContentLength(13);
    header.setValue("Connection", "Keep-Alive");
    header.setValue("Referer", "Referer=http://localhost:1048/MyWebsite/GetManagers.asmx?op=Portfolios");

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

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


    So, I think that I am already handling the event loop correctly because of the following:
    1. This is a GUI application, so there is an event loop.
    2. Class, that handles http POST request is a RemoteManagers class that is instantiated in CMainView class.
    3. I copied values for header settings from the actual request to the webservice that I capture in the debug mode for webservice when request comes from the browser.

    m_http.request does not even reach the Web Server (I am running it in the debug mode, and when I access the service via the browser I am hitting a breakpoint...).

    Any input is greatly appreciated

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

    Default Re: POST request to a web service

    Try to connect slot to signal before QHttp::request() called.

Similar Threads

  1. Creating QDS service
    By bowser in forum Qt for Embedded and Mobile
    Replies: 1
    Last Post: 29th October 2007, 11:12
  2. Https POST Request
    By munna in forum Qt Programming
    Replies: 10
    Last Post: 11th November 2006, 14: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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.