Sending http request synchronously in Metro style apps RRS feed

  • Question

  •  Sending HTTP request and receiving response using XMLHTTPRequest2 in Metro style apps (c++) is asynchronous. After you have requested a page, it does not wait until you get the response. Code in between sending and receiving gets executed. Is there any synchronous method to send http request / get the response?  
    Tuesday, May 15, 2012 1:52 PM


All replies

  • Take a look at Asynchronous programming in C++ documentation for information on how to deal with asynchronous functions easily. If you use PPL you can use task::then to readily set up a completion sequence so your response to the async function gets called when it completes.


    Wednesday, May 16, 2012 6:10 AM
  • The IXMLHTTPRequest2 interface happens to be an asynchronous one. There is another interface

    IServerXMLHTTPRequest which can also be used to send http requests. But can this interface be used in Metro style apps? Or is there any other interface that responds synchronously in Metro style apps?

    Thursday, May 17, 2012 1:47 PM
  • Thank you Rob!!

    but when we call XMLHTTPRequest2::Open or send, the reply comes in a callback. There will be various callback overrides like OnDatareceived(), OnResponseRecived() etc. Since the callbak event happen in another class derived from the callback class, how will we know that when the request has completed? Or can we take that OnResponseRecived() is the last function?

    Is it possible to call like below and ensure that the RequestResource() function will return only after the http request has completed and response received ( the task statement is skeleton only in the below code: Here I need to call the open()/send method synchronously using task<>.

    void BlankPage::RequestResource( String^ strIP, String^ strMethod )
        HRESULT hrSend = CoCreateInstance(CLSID_FreeThreadedXMLHTTP60, nullptr, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&m_HttpRequest));
        if (SUCCEEDED(hrSend))
            ComPtr<IXMLHTTPRequest2Callback> CallBack = Microsoft::WRL::Details::Make<CallBackHandler>(
            ref new XHRDataReceivedHandler(this, &BlankPage::OnDataReceived),        
            ref new XHRFailedHandler(this, &BlankPage::OnFailed),
            ref new XHRHeadersAvailableHandler( this,&BlankPage::RequestResource )

    task<void> t([]...)


            hrSend = m_HttpRequest->Open( strMethod->Data(), strIP->Data(), CallBack.Get(), nullptr, nullptr, nullptr, nullptr);
            if (SUCCEEDED(hrSend))
                if (SUCCEEDED(hrSend))
                    hrSend = m_HttpRequest->Send(NULL,0);

        if (FAILED(hrSend))




    Also, alternatively is it OK to synchronise using a synchronisation object like event handle?
    Thursday, May 17, 2012 6:25 PM
  • You really don't want to force this to be synchronous on your UI thread. That can make your application freeze and really frustrate your users.

    I've pointed our web programming specialist at this thread to see if he can give more concrete guidance.


    Friday, May 18, 2012 12:25 AM
  • Hi all,

    I have same question for using XMLHTTPRequest2.
    Is there any update for this question?


    Wednesday, June 6, 2012 8:02 AM
  • Hi Woody,

    Don't use async in your UI threads.

    There is an example of using XHR C++ in this sample:


    See the file XMLHTTPRequest2Callback.cpp for a good wrapper around XHR for C++!


    Jeff Sanders (MSFT)

    Wednesday, June 6, 2012 3:27 PM
  • Hi jpsanders,

    Why I need use async in UI threads?
    Is there any limitation for using other thread to send http request / get the response

    Depend on its_me_here's reply and sample code.
    Because the XMLHTTPRequest2 just support callback function to implement it.
    He just want follow the task.then model to implement that using XMLHTTPRequest2 to send http request / get the response.

    So, the question is talking about:
    " Is there and method can follow the task.then model to implement send http request / get the response"




    Thursday, June 7, 2012 3:08 AM
  • No sample I know except for the one I listed above!

    Jeff Sanders (MSFT)

    Tuesday, June 12, 2012 5:37 PM
  • It is always better to handle each possible return value from the http request in the callback and proceed accordingly. The possible vales like error, if page was found etc. These can be obatined at the overrides of the callbacks..That would be better than programatically waiting until a send request is complete. Let the httprequest2 method tell u what happened for a request and do accordingly. Thank you guys!!
    Wednesday, June 13, 2012 5:08 AM