locked
Sending an HTTP request via C++ / WinRT

    Question

  • In C#, there is the HttpWebRequest which can be used to issue an HTTP request (GET, POST, PUT, DELETE).

    I'm looking to build a metro-style app in C++. Is there a high-level WinRT API that has similar functionality as HttpWebRequest or will I have to rely on sending requests to web sockets directly?

    Thanks!

    stu

    Thursday, October 20, 2011 2:27 AM

Answers

  • Hi,

    As of now there is no WinRT API for doing this.  Even C# and JScript make use of functionality that comes with their respective frameworks (.NET Framework and WinJS respectively).

    I agree that having a common WinRT API for all languages would be the best thing.  But for now you may want to look at some existing C++ ways of doing this:

    • http://cpp-netlib.github.com/ : This is proposed to be a part of Boost
    • http://pocoproject.org/ : They have a good Http Client

    Thanks
    Raman 

    Tuesday, November 01, 2011 7:44 PM

All replies

    • Marked as answer by stugreco Friday, October 21, 2011 3:55 AM
    • Unmarked as answer by stugreco Monday, November 07, 2011 9:16 AM
    Thursday, October 20, 2011 3:08 AM
  • Hi Raman,  I didn't find that thread helpful.  I tried the Background download class but it didn't work.  Will WinRT have a high level API like HttpWebRequest?
    Tuesday, November 01, 2011 5:35 PM
  • Hi,

    As of now there is no WinRT API for doing this.  Even C# and JScript make use of functionality that comes with their respective frameworks (.NET Framework and WinJS respectively).

    I agree that having a common WinRT API for all languages would be the best thing.  But for now you may want to look at some existing C++ ways of doing this:

    • http://cpp-netlib.github.com/ : This is proposed to be a part of Boost
    • http://pocoproject.org/ : They have a good Http Client

    Thanks
    Raman 

    Tuesday, November 01, 2011 7:44 PM
  • I don't see how we can use either library Poco's http client in a Metro-style application.  The code uses Winsock, but Winsock isn't available in Metro style applications. winsock2.h only does things if the app is being compiled as WINAPI_PARTITION_DESKTOP and also I know for a fact that a lot of the winsock functions are blacklisted (see this link for my results when trying to validate an application that uses OpenSSL, which uses Winsock).  Raman, can you explain why you think that we can use Poco?  Have you gotten it to work?

    cpp-netlib also seems unlikely to work, as https support requires OpenSSL which uses sockets.

    • Edited by Adam Gross Thursday, March 15, 2012 4:25 PM
    Thursday, March 15, 2012 3:03 PM
  • Now that Visual Studio 2012 RTM build has officially come out what is the best way to make na HTTP Request from C++ in WinRT?

    • Proposed as answer by SadCatSoft Monday, August 20, 2012 7:09 PM
    • Unproposed as answer by SadCatSoft Monday, August 20, 2012 7:09 PM
    Monday, August 20, 2012 7:01 PM
  • We took a look at the above libraries, but actually trying them didn't go so well for us. One required dependencies on WinSock which were explicitly disallowed for Metro apps (also, compiling Boost is no fun) and the other seemed to have serious dependencies on OpenSSL - which for our very simple goals was a bit much.

    We ended up writing our own C# .winmd component that performs the http request and returns the result back to our C++ app. The C# part is well-documented here: http://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh781239.aspx

    So far this approach seems to work pretty well all the way through RTM, and doesn't require third-party libraries.

    Monday, August 20, 2012 7:16 PM
    • Proposed as answer by Adam Gross Wednesday, August 22, 2012 7:26 PM
    Tuesday, August 21, 2012 9:15 PM
  • Yeah IXMLHTTPRequest2 is really all we can use in Metro.  All the third-party stuff won't compile because it was written for Winsock.  This kind of stuff makes me wish that Microsoft had employed the carrot more than the stick and allowed everyone to use Winsock as well but only allowed background transaction support for the newer socket APIs.  Not being able to use Winsock makes a fairly simple Metro port so much more complicated.
    • Proposed as answer by Adam Gross Thursday, December 13, 2012 7:12 PM
    Wednesday, August 22, 2012 7:28 PM
  • I ended up doing this as well. Thanks!

    Thursday, August 23, 2012 2:51 PM
  • Dave, does IXMLHTTPRequest2 work with binary content? example, calling an end point that returns a binary file?
    Thursday, August 23, 2012 2:53 PM
  • Yes it does.   Scenario 2 in the sample uses binary content (which the sample just hex encodes and displays).
    For retrieving large binary files though, you may also consider using BackgroundTransfer, which has a sample at http://code.msdn.microsoft.com/windowsapps/Background-Transfer-Sample-d7833f61

    • Proposed as answer by Sugudo Friday, December 28, 2012 10:32 AM
    Thursday, September 27, 2012 7:28 PM
  • Hi all,

    I want to use httpclient c++ for windows phone 8.Windows 8 had sample httpclient for Windows 8 style, but I didn't implement for Windows phone 8.

    who can you help I ?

    please !

    Thanks

    Friday, December 28, 2012 10:42 AM
  • New in Windows 8.1: the Windows.Web.Http HttpClient API.  This is an easy-to-use, full-featured HTTP API that works in all languages: C++, .NET (C#, VB) and JavaScript.    Advantages of the new API include: strongly typed headers (meaning: you write fewer bugs in less time), full support for standard WinRT async concepts and data types.

    HttpClient also lets you inject your modular filter code into the HTTP processing pipeline, letting you handle logging, testing, retry and auth more naturally.  Samples of filters include retry and metered network filters in the HttpClient sample, and OAUTH and OAUTH 2.0 support in the Web Authentication Broker sample.

    (Note that the existing IXHR2 code is still available for developers, and your existing Windows 8 code should continue to work as expected)

    There's a //build/ 2013 video for the new API at http://channel9.msdn.com/Events/Build/2013/4-092

    We also have samples at http://code.msdn.microsoft.com/windowsapps/HttpClient-sample-55700664

    There's an OAUTH 0 filter sample at http://code.msdn.microsoft.com/windowsapps/Web-Authentication-d0485122

    And there's documentation at http://msdn.microsoft.com/en-us/library/windows/apps/windows.web.http.aspx


    Network Developer Experience Team (Microsoft)

    Tuesday, July 16, 2013 9:28 PM