locked
HTTP GET not working in Metro C++ app using IXMLHTTPRequest2

    Question

  • I am having trouble getting any sort of response or error when using IXMLHTTPRequest2 in our Metro C++ application.  This is what I'm doing (error handling omitted for space reasons):

    HRESULT hr;
    
    DWORD dwStatus = 0;
    ComPtr<IXMLHTTPRequest2> xhr;
    ComPtr<CXMLHttpRequest2Callback> xhrMyCallback;
    ComPtr<IXMLHTTPRequest2Callback> xhrCallback;
    
    hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
    hr = CoCreateInstance(CLSID_FreeThreadedXMLHTTP60, nullptr, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&xhr));
    hr = MakeAndInitialize<CXMLHttpRequest2Callback>(&xhrMyCallback);
    hr = xhrMyCallback.As(&xhrCallback);
    hr = xhr->Open(L"GET", L"http://www.google.com", xhrCallback.Get(), NULL, NULL, NULL, NULL);
    hr = xhr->Send(NULL, 0);
    hr = xhrMyCallback->WaitForComplete(&dwStatus);

    For the CXMLHttpRequest2Callback, I'm using the code from this example:  XML HTTP Request 2 GET sample

    After calling WaitForComplete(), the callback never receives the OnError() or OnResponseReceived() events.  I have enabled the 'Internet Client' permission in the manifest file.

    What am I misssing?

    Thanks!


    Tuesday, July 24, 2012 8:25 PM

All replies

  • try add a message pump to the wait.


    The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
    Visual C++ MVP

    Tuesday, July 24, 2012 9:30 PM
  • Are your ComPtr objects going out-of-scope?  Does WaitForComplete block?  I have used this in both the sample and adapted to another metro app without any problem.
    Tuesday, October 02, 2012 5:02 PM
  • Most likely, you're dropping your last refcount on your IXMLHTTPRequest2 ("xhr" in your example), which aborts the request, probably well before it had a chance to do anything interesting.

    In other words, hang onto that guy until your request has completed or failed.


    This posting is provided AS IS with no warranties and confers no rights. -- Becky Weiss

    • Proposed as answer by Helin Wang Monday, November 26, 2012 9:08 PM
    Thursday, November 22, 2012 3:16 AM
  • I'm doing the exact same thing as rknoesel, and getting the same problem, none of the callbacks ever return and WaitForSingleObjectEx (in CXMLHttpRequest2Callback::WaitForComplete) never returns.

    The strange thing is fiddler shows that I'm not only making the request, but I'm getting the full response (including http 200 OK), so the Send request worked, I just never find out one way or another in the code.

    I don't see how the refcount of xhr is an issue, since the ComPtr owning xhr is still on the stack (since WaitForComplete hasn't returned). The Comptr's are still in scope and WaitForComplete is blocking.

    Any help would be really appreciated...

    Tuesday, December 11, 2012 9:51 PM
  • If you make the XHR2->Send call from your UI thread, all the callbacks come in on your UI thread. So, if you call XHR2->Send on your UI thread and then call WaitForSingleObjectEx on the UI thread, the Wait will never complete because the callbacks are blocked. You can call XHR2->Send on the UI thread (I think MSFT doesn't recommend this because it can be a long operation) but you can't Wait on its results, you would have to poll it for completion or have your callbacks, in effect, handle the completion tasks. It all depends on how your app handles waiting for the download to complete (does it disable parts of the UI?). My Metro app's UI updated its data in the background so I put all the XHR2 stuff in a ThreadPool thread where Waiting on the result was fine. If the download in your Metro app is interactive then you'll need to disable parts of the UI while downloading to prevent users from triggering multiple downloads simultaneously.

    • Proposed as answer by rbarr Wednesday, December 12, 2012 9:58 PM
    Wednesday, December 12, 2012 5:08 AM
  • download win8 store app sample code, it works very well.

    rob qqq

    Wednesday, December 12, 2012 6:36 PM