HTTP WinRT Client (for C++)
To really start digging into the Metro style apps in C++, I wanted to make a simple component. Currently, there are two ways of making generic HTTP calls from C++ in a Metro application. One is IXMLHTTPRequest2 and the other is Casablanca. IXMLHTTPRequest2 is not really that great of an API. Casablanca is really going to be awesome and suggest folks try it out. When it’s released I’m positive it’ll be the preferred way of doing HTTP in C++. It is pre-beta and the API is going to change in coming releases.
So why did I make this? As a learning exercise. There’s a lot of concepts I wanted to learn and figured a WinRT wrapper around IXMLHTTPRequest2 would be full of surprises.
In the code I have a few things of interest:
- A custom IBuffer implementation made with WRL (Microsoft::WRL::RuntimeClass). WRL will let us mix old style COM interfaces with a runtime object. It’s very well made and way easier than ATL.
- CoCreateInstanceFromApp usage. This is the Metro version of CoCreateInstance and will let you use your own registration free COM objects and Metro approved COM classes.
- Lots of WRL ComPtr. Never use raw COM interface pointers unless you know what you are doing! This will ensure you never miss an AddRef/Release
- WeakReferences. A Circular reference is the Achilles heal for reference counted objects! Learn when to use WeakReferences
I implemented the standard /CX events for callbacks. The new async model (the impl is still async) would have been better, so one could use the continuations or async/await, but I’ll tackle that in another project.
One note, in the sample application I seemed to have odd perf problems with long strings in a XAML text box, so you will see I hard-coded the string to be no more than 200 chars.
As always, this is a prototype for learning and fun. Since I’m still learning, there could be mistakes, bugs and what not ![]()
Here’s the code!
Hello~~I used this sample in Windows 8 Release Preview And VS 2012 RC will have some error!!
Could you update this sample code!!
Your sample can save me!
Thanks you very much
Hi,
you example doesn’t compile on my machine (Windows 8 + VS2012 RC) :
Error 1 error C2456: ‘NetworkUtilities::HttpDataResult’ an unsealed ref class cannot have a public visible constructor. Seal the class, or specify constructor as ‘internal’, ‘protected private’ or ‘private’ instead. (HttpClient.cpp) c:\users\j.doe\downloads\httpsample\networkutilities\HttpDataResult.h 15 NetworkUtilities
Error 2 error C3986: ‘{ctor}’: signature of public member contains native type ‘Microsoft::WRL::ComPtr’ c:\users\j.doe\downloads\httpsample\networkutilities\HttpDataResult.h 15 NetworkUtilities
Error 3 error C3986: ‘{ctor}’: signature of public member contains native type ‘ISequentialStream’ (HttpClient.cpp) c:\users\j.doe\downloads\httpsample\networkutilities\HttpDataResult.h 15 NetworkUtilities
Error 4 error C3987: ‘{ctor}’: signature of public member contains native type ‘Microsoft::WRL::ComPtr &’ c:\users\j.doe\downloads\httpsample\networkutilities\HttpDataResult.h 15 NetworkUtilities
Error 5 error C2456: ‘NetworkUtilities::HttpDataResult’ an unsealed ref class cannot have a public visible constructor. Seal the class, or specify constructor as ‘internal’, ‘protected private’ or ‘private’ instead. (HttpCallbackProxy.cpp) c:\users\j.doe\downloads\httpsample\networkutilities\HttpDataResult.h 15 NetworkUtilities
Error 6 error C3986: ‘{ctor}’: signature of public member contains native type ‘Microsoft::WRL::ComPtr’ c:\users\j.doe\downloads\httpsample\networkutilities\HttpDataResult.h 15 NetworkUtilities
Error 7 error C3986: ‘{ctor}’: signature of public member contains native type ‘ISequentialStream’ (HttpCallbackProxy.cpp) c:\users\j.doe\downloads\httpsample\networkutilities\HttpDataResult.h 15 NetworkUtilities
Error 8 error C3987: ‘{ctor}’: signature of public member contains native type ‘Microsoft::WRL::ComPtr &’ c:\users\j.doe\downloads\httpsample\networkutilities\HttpDataResult.h 15 NetworkUtilities
Error 9 error C2456: ‘NetworkUtilities::HttpDataResult’ an unsealed ref class cannot have a public visible constructor. Seal the class, or specify constructor as ‘internal’, ‘protected private’ or ‘private’ instead. (HttpDataResult.cpp) c:\users\j.doe\downloads\httpsample\networkutilities\HttpDataResult.h 15 NetworkUtilities
Error 10 error C3986: ‘{ctor}’: signature of public member contains native type ‘Microsoft::WRL::ComPtr’ c:\users\j.doe\downloads\httpsample\networkutilities\HttpDataResult.h 15 NetworkUtilities
Error 11 error C3986: ‘{ctor}’: signature of public member contains native type ‘ISequentialStream’ (HttpDataResult.cpp) c:\users\j.doe\downloads\httpsample\networkutilities\HttpDataResult.h 15 NetworkUtilities
Error 12 error C3987: ‘{ctor}’: signature of public member contains native type ‘Microsoft::WRL::ComPtr &’ c:\users\j.doe\downloads\httpsample\networkutilities\HttpDataResult.h 15 NetworkUtilities
Error 13 error C1192: #using failed on ‘C:\Users\j.doe\Downloads\HttpSample\Release\NetworkUtilities\NetworkUtilities.winmd’ C:\Users\j.doe\Downloads\HttpSample\HttpSampleApp\pch.cpp 1 HttpSampleApp
Same errors here. I tried sealing the class but that’s the least of the problems. Too bad I would LOVE a wrapper from IXML2
if you make the constructor of HttpDataResult private and a HttpCallbackProxy a friend , then it should work.
I almost fixed all errors but there is a crash at runtime, I let you fix the problem as an exercise
http://www.smartmobili.com/downloads/HttpSample_RTM.zip
Did you manage to fix the runtime error? I’m really too much of a novice to get this to work (the problem has something to do with calling conventions, but I’ve no clue of where to start fixing it).
Nevermind… got it working. Just remove the “&” in the Lambda (so new DispatchedHandler([this, reader]) instead of new DispatchedHandler([this, &reader]), Moving all calls outside the DispatchedHandler also works but is suboptimal. Thanks for sharing.