locked
How to control HTTP Version when use HttpClient class?

Answers

  • For the original question -- about the Windows.Web.Http classes -- there is no control over the HTTP version as sent.    However, HTTP 1.1 is very compatible with HTTP 1.0; all HTTP 1.0 servers should be able to handle our HTTP 1.1 traffic appropriately.  In the other direction (where you want to ensure HTTP 1.1 traffic only), you actually might not be able to guarantee that the traffic is always 1.1: some of your customers might be behind gateways that convert HTTP 1.1 traffic into HTTP 1.0 traffic.

    The enum you see is returned on the HttpResponseMessage, and tells you the HTTP version number that the server replied with.

    Can you explain why you need to control the version number?  Are you connecting to a server that's doesn't gracefully handle HTTP 1.1 traffic?


    Network Developer Experience Team (Microsoft)

    • Marked as answer by Lattimore Tuesday, October 22, 2013 7:45 AM
    Friday, October 18, 2013 10:02 PM
  • For Windows.Web.Http, the HTTP version is actually under the control of the (super sophisticated) WinINet http stack.  WinINet is what IE uses.  IIRC, it essentially "always" uses 1.1

    HttpWebRequest is indeed a C# class; we recommend that you upgrade your app to use the Windows.Web.Http HttpClient family of classes.  These classes generally give you more control over the overall HTTP processing while also being easier to use.

    We have a poster that demonstrates the overall set of classes in the Windows.Web.Http namespace; you can download it here; there's also a //build/ 2013 presentation here


    Network Developer Experience Team (Microsoft)

    • Marked as answer by Lattimore Wednesday, October 23, 2013 2:23 AM
    Tuesday, October 22, 2013 5:34 PM

All replies

  • Hi Lattimore Neu,

    Welcome to MSDN forum!

    The HttpVersion class defines the HTTP versions that are supported by the HttpWebRequest and HttpWebResponse classes.

    HttpClient doesn't provide the function to get HTTP Version. But you could get it by HttpWebRequest class. And you could get how to use the HttpVersion enumeration.

    Please try the code shown below:

    // Create a 'HttpWebRequest' object. HttpWebRequest^ myHttpWebRequest = (HttpWebRequest^)( WebRequest::Create( "http://www.microsoft.com" ) ); Console::WriteLine( "\nThe 'ProtocolVersion' of the protocol before assignment is : {0}", myHttpWebRequest->ProtocolVersion ); // Assign Version10 to ProtocolVersion. myHttpWebRequest->ProtocolVersion = HttpVersion::Version10; // Assign the response Object* of 'HttpWebRequest' to a 'HttpWebResponse' variable. HttpWebResponse^ myHttpWebResponse = (HttpWebResponse^)( myHttpWebRequest->GetResponse() ); Console::WriteLine( "\nThe 'ProtocolVersion' of the protocol after assignment is : {0}", myHttpWebRequest->ProtocolVersion ); Console::WriteLine( "\nThe 'ProtocolVersion' of the response Object* is : {0}", myHttpWebResponse->ProtocolVersion );


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Friday, October 18, 2013 7:44 AM
    Moderator
  • For the original question -- about the Windows.Web.Http classes -- there is no control over the HTTP version as sent.    However, HTTP 1.1 is very compatible with HTTP 1.0; all HTTP 1.0 servers should be able to handle our HTTP 1.1 traffic appropriately.  In the other direction (where you want to ensure HTTP 1.1 traffic only), you actually might not be able to guarantee that the traffic is always 1.1: some of your customers might be behind gateways that convert HTTP 1.1 traffic into HTTP 1.0 traffic.

    The enum you see is returned on the HttpResponseMessage, and tells you the HTTP version number that the server replied with.

    Can you explain why you need to control the version number?  Are you connecting to a server that's doesn't gracefully handle HTTP 1.1 traffic?


    Network Developer Experience Team (Microsoft)

    • Marked as answer by Lattimore Tuesday, October 22, 2013 7:45 AM
    Friday, October 18, 2013 10:02 PM
  • Hi, 

    Thanks for your answer. HttpWebRequest is a C# class. How can I reference it from my WinRT component writen in C++/Cx?

    Tuesday, October 22, 2013 7:56 AM
  • Hi,

    Thanks for your suggestion. I just try to find how to control HTTP version but not necessary. My problem is that:

    In Windows 8.0, there is no API implementing HTTP request in WinRT. And my WinRT component is writen in C++/CX, I cannot use C# APIs. At last, I use BackgroundDownloader API to download remote resources. But when I use WireShark to sniff my app's network. I find that all HTTP request were under version 1.0. My app access Internet frequently, the connection's establishment and destroy may consume much resource. 

    In Windows 8.1, I can use  HttpClient class replace BackgroundDownloader. I try to find a way to ensure that my Internet connection is not established again and again. As you say, if there is no way to control the HTTP version, how HttpClient class determines that a HTTP request will use 1.0 or 1.1?


    • Edited by Lattimore Tuesday, October 22, 2013 8:24 AM
    Tuesday, October 22, 2013 8:23 AM
  • For Windows.Web.Http, the HTTP version is actually under the control of the (super sophisticated) WinINet http stack.  WinINet is what IE uses.  IIRC, it essentially "always" uses 1.1

    HttpWebRequest is indeed a C# class; we recommend that you upgrade your app to use the Windows.Web.Http HttpClient family of classes.  These classes generally give you more control over the overall HTTP processing while also being easier to use.

    We have a poster that demonstrates the overall set of classes in the Windows.Web.Http namespace; you can download it here; there's also a //build/ 2013 presentation here


    Network Developer Experience Team (Microsoft)

    • Marked as answer by Lattimore Wednesday, October 23, 2013 2:23 AM
    Tuesday, October 22, 2013 5:34 PM
  • Thank you very much. I will use HttpClient as you suggested.
    Wednesday, October 23, 2013 2:22 AM