{"id":843,"date":"2013-07-17T17:50:00","date_gmt":"2013-07-17T17:50:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/dotnet\/2013\/07\/17\/httpclient-2-2-is-now-stable\/"},"modified":"2021-10-04T12:28:08","modified_gmt":"2021-10-04T19:28:08","slug":"httpclient-2-2-is-now-stable","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/dotnet\/httpclient-2-2-is-now-stable\/","title":{"rendered":"HttpClient 2.2 is now stable"},"content":{"rendered":"<p>As <a href=\"http:\/\/blogs.msdn.com\/b\/dotnet\/archive\/2013\/07\/10\/httpclient-2-2-is-now-rc.aspx\">promised<\/a>, we&rsquo;ve just pushed the RTM version of <a href=\"http:\/\/nuget.org\/packages\/Microsoft.Net.Http\/\">HttpClient 2.2 to NuGet<\/a>. This version adds support for automatic decompression. As <a href=\"http:\/\/blogs.msdn.com\/b\/dotnet\/archive\/2013\/06\/19\/update-to-httpclient-and-automatic-decompression.aspx\">announced here<\/a>, this doesn&rsquo;t require a dependency on <a href=\"http:\/\/nuget.org\/packages\/Microsoft.Bcl.Compression\/\">Microsoft.Bcl.Compression<\/a>. This allowed us to support automatic decompression on more platforms and without forcing your projects to change the CPU architecture.<\/p>\n<p>It&rsquo;s worth noting that we didn&rsquo;t make any changes since the <a href=\"http:\/\/blogs.msdn.com\/b\/dotnet\/archive\/2013\/07\/10\/httpclient-2-2-is-now-rc.aspx\">RC version we published a week ago<\/a> (despite changing the version).<\/p>\n<h2>How to use automatic decompression<\/h2>\n<p><em>(For the sake of keeping this post self-contained I&rsquo;ve copied the content from the post that <a href=\"http:\/\/blogs.msdn.com\/b\/dotnet\/archive\/2013\/06\/06\/portable-compression-and-httpclient-working-together.aspx\">introduced automatic decompression<\/a>)<\/em><\/p>\n<p>First, automatic decompression is not enabled by default for <strong>HttpClient<\/strong>. To use it, you need to set the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.net.http.httpclienthandler.automaticdecompression.aspx\">AutomaticDecompression property<\/a> on <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/hh193540.aspx\">HttpClientHandler to GZip or Deflate<\/a>. This API follows the optional feature pattern; meaning it exposes an API that indicates whether a particular feature is supported. Automatic decompression support is indicated with the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.net.http.httpclienthandler.supportsautomaticdecompression.aspx\">SupportsAutomaticDecompression property<\/a>.<\/p>\n<p>If an implementation of <strong>HttpClient<\/strong> doesn&rsquo;t support automatic decompression, it returns <strong>false<\/strong> from the <strong>SupportsAutomaticDecompression<\/strong> property and throws a <strong>NotSupportedException<\/strong> from the setter and getter for the AutomaticDecompression property.<\/p>\n<p>With this pattern in mind, using automatic decompression looks like this:<\/p>\n<pre style=\"padding-left: 30px\">var handler = new HttpClientHandler();\nif (handler.SupportsAutomaticDecompression)\n{\n    handler.AutomaticDecompression = DecompressionMethods.GZip |\n                                     DecompressionMethods.Deflate;\n}\nvar httpClient = new HttpClient(handler);\nvar str = await httpClient.GetStringAsync(\"http:\/\/en.wikipedia.org\/wiki\/Gzip\");\n<\/pre>\n<p>The request then provides the additional Accept-Encoding header:<\/p>\n<pre style=\"padding-left: 30px\">GET http:\/\/en.wikipedia.org\/wiki\/Gzip HTTP\/1.1\nHost: en.wikipedia.org\n<span style=\"background-color: #ffff00\">Accept-Encoding: gzip, deflate<\/span>\nConnection: Keep-Alive\n<\/pre>\n<p>Servers that choose to support it will respond and indicate the compression algorithm they used. In this example, the server compressedt he body using gzip:<\/p>\n<pre style=\"padding-left: 30px\">HTTP\/1.1 200 OK\nServer: nginx\/1.1.19\nDate: Wed, 13 Mar 2013 14:04:24 GMT\nContent-Type: text\/html; charset=UTF-8\nContent-Length: 17765\nConnection: keep-alive\nX-Content-Type-Options: nosniff\nContent-Language: en\nLast-Modified: Tue, 05 Mar 2013 03:38:51 GMT\n<span style=\"background-color: #ffff00\">Content-Encoding: gzip<\/span>\nExpires: Thu, 01 Jan 1970 00:00:00 GMT\nCache-Control: private, s-maxage=0, max-age=0, must-revalidate\nVary: Accept-Encoding,Cookie\nAge: 179\n<\/pre>\n<p>The <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.net.http.httpcontent.createcontentreadstreamasync.aspx\">response stream<\/a> that <strong>HttpClient<\/strong> returns to you will automatically decompress the result using the appropriate algorithm. On my machine I got the following results:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2013\/07\/7360.Graph_.png\" alt=\"Content-Length http:\/\/en.wikipedia.org\/wiki\/Gzip\" \/><\/p>\n<p><strong>This yields to a reduction by 77%<\/strong>. Needless to say, the actual results depend on the request and how well it compresses. So your results will naturally vary.<\/p>\n<h2>Summary<\/h2>\n<p>We&rsquo;ve just shipped the RTM version of HttpClient 2.2, which updates <a href=\"http:\/\/nuget.org\/packages\/Microsoft.Net.Http\/\">HttpClient<\/a> by adding support for automatic decompression. It&#8217;s supported on .NET 4.0, Windows Phone 7.5, and partially supported on Silverlight (see this <a href=\"http:\/\/blogs.msdn.com\/b\/dotnet\/archive\/2013\/06\/19\/update-to-httpclient-and-automatic-decompression.aspx#silverlight\">blog post for details<\/a>).<\/p>\n<p>Thanks for all the feedback we&rsquo;ve received. We&rsquo;d love to hear how you like the new version!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>As promised, we&rsquo;ve just pushed the RTM version of HttpClient 2.2 to NuGet. This version adds support for automatic decompression. As announced here, this doesn&rsquo;t require a dependency on Microsoft.Bcl.Compression. This allowed us to support automatic decompression on more platforms and without forcing your projects to change the CPU architecture. It&rsquo;s worth noting that we [&hellip;]<\/p>\n","protected":false},"author":335,"featured_media":58792,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[685],"tags":[30,43,104,110],"class_list":["post-843","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-dotnet","tag-announcement","tag-bcl","tag-nuget","tag-portable-class-libraries"],"acf":[],"blog_post_summary":"<p>As promised, we&rsquo;ve just pushed the RTM version of HttpClient 2.2 to NuGet. This version adds support for automatic decompression. As announced here, this doesn&rsquo;t require a dependency on Microsoft.Bcl.Compression. This allowed us to support automatic decompression on more platforms and without forcing your projects to change the CPU architecture. It&rsquo;s worth noting that we [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts\/843","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/users\/335"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/comments?post=843"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts\/843\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/media\/58792"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/media?parent=843"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/categories?post=843"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/tags?post=843"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}