locked
Sending Http request in Metro Style apps

    Question

  • I would like to know how we can send http request to a resource( say a web page) and to get the response from the request in c++ Metro style app. I need to inspect the response. This is like the normal http request we do with  HttpOpenRequest() API in wininet. I found an interface  IXMLHTTPRequest2 which does the same in Metro style app. This requires callback to get notifications from the server. I found a sample in the Samples collection which handles push notifications. But this requires uploading to server. But I think in order to send a simple  http request to another machine, uploading is not needed. Any info about how this can be simply done using IXMLHTTPRequest2  is appreciated. I also found that IXMLHTTPRequest is mainly done in Multithreaded apartment. Hope there is no problem in using this in a single threaded app.

    Wednesday, May 09, 2012 3:04 PM

Answers

  • You sure can use sockets (you would have hit the same issue with the Private networks capability by the way)!  The HTTP protocol uses sockets to establish the communication and transfer the HTTP data.  I will not go through how to implement the HTTP protocol manually however and use sockets, that would take much longer than you could devote to a forum post.

    -Jeff


    Jeff Sanders (MSFT)

    Thursday, May 10, 2012 2:46 PM
    Moderator

All replies

  • Hello,

     

    Thanks for your feedback, I will involve more experts to investigate it.

     

    Best regards,

    Jesse


    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us

    Thursday, May 10, 2012 6:58 AM
  • Hi,

     I am able to send http request and fetch pages using the above method. ie. I am getting response from the requested IP and can see the page contents. But it fails to fetch pages from IPs that are within the subnet of the machine sending the request. In such cases, it says "The system cannot locate the specified resource". This is caught by the OnError() method of ?IXMLHTTPRequest2CallBack. I am using the below code( skeleton only given )

    IXMLHTTPRequest2Callback callBack;
    IXMLHTTPRequest2 req;
    HRESULT hr = req->Open(L"GET", "http://xx.xx.xxx.xxx", callBack.Get(), nullptr, nullptr, nullptr, nullptr);

    ie.If I am sending the above request from a PC say "XXX.XX.XX.53 " and request a page like say "http://XXX.XX.XX.78" then it says "The system cannot locate the specified resource". Here the X parts are the same in both IPs.  Why is it that it can access outside and not access from within? Any inputs?

    Thursday, May 10, 2012 12:28 PM
  • Hi Its,

    It sounds like you have some sort of proxy issue!  Can you access the same exact URL from internet explorer on that same system?  And no, sockets would be much more difficult as you would have to implement the HTTP Protocol in your own code then.

    -Jeff


    Jeff Sanders (MSFT)

    Thursday, May 10, 2012 2:24 PM
    Moderator
  • Well, that issue is solved when I added the Private Networks Capability to my app. Now I am able to get the pages in my app. Oops!! I had missed to add this capability!! The devices I was able to access earlier  were wireless devices actually and I was able to access them may be becoz the Internet Capability was set by default.  Anyway I would like to know if we can fetch information like the above using sockets..But the IXMLHttpRequest approach looks easier..

    Thursday, May 10, 2012 2:38 PM
  • You sure can use sockets (you would have hit the same issue with the Private networks capability by the way)!  The HTTP protocol uses sockets to establish the communication and transfer the HTTP data.  I will not go through how to implement the HTTP protocol manually however and use sockets, that would take much longer than you could devote to a forum post.

    -Jeff


    Jeff Sanders (MSFT)

    Thursday, May 10, 2012 2:46 PM
    Moderator
  • Does that mean using sockets is better and easier than HTTP protocol using IXMLHTTPRequest?

    In my case , I am sending http request to all IPs within that subnet. My idea is to detect network printers connected in that network. I think socket communication needs a specific dedicated port at which that particular device should be listening. But your device cannot be expected to listen in that port always. If another device is listening at that particular port, that device will also show up. Thats why I decided to send https request to all IPs and inspect the response headers. But one thing is,  this is asynchronous communication and you wont know when you will get a reply. This could be an issue when you are sending request to multiple IPs.

    Friday, May 11, 2012 4:23 AM
  • The HTTP protocol is easier.


    Jeff Sanders (MSFT)

    Friday, May 11, 2012 8:16 PM
    Moderator
  • 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:33 PM