1 / 35

Five great reasons to use the new HttpClient API

Five great reasons to use the new HttpClient API. Peter Smith Program Manager 4-092. 5 top reasons to use the new HttpClient API. C++. C#. JavaScript. Reason. new!. ✔. ✔. Shared cache, cookies, credentials. new!. new!. ✔. Strongly typed headers=fewer bugs in less time. new!.

wilmet
Download Presentation

Five great reasons to use the new HttpClient API

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Five great reasons to use the new HttpClient API Peter Smith Program Manager 4-092

  2. 5 top reasons to use the new HttpClient API C++ C# JavaScript Reason new! ✔ ✔ Shared cache, cookies, credentials new! new! ✔ Strongly typed headers=fewer bugs in less time new! new! ✔ Access to cookies and shared cookies new! new! new! Control over caching and shared cache new! ✔ new! Inject your code modules into the processing pipeline=cleaner, more modular code

  3. Meet the family! HttpClient (has common send methods) Http Base Protocol Filter (has in-depth settings) Your App REST/Web Service HttpRequestMessage HttpResponseMessage ⛭ HttpContent String • Stream • Buffer • Multipart • FormUrlEncoded

  4. Simple example • try • { • var uri = newUri("http://example.com/datalist.aspx"); • var httpClient = newHttpClient(); • var result = await httpClient.GetStringAsync(uri); • } • catch (Exception e) • { • }

  5. Let’s see it in action!

  6. Reason #1:HTTP Integration: cache, cookies, credentials…

  7. Cache, cookies, credentials are already shared between <WebView>, <Image>, <Media>, …These shared with Windows.Web.Http, too(but not shared across apps)

  8. Reason #2:Strongly typed headers: fewer bugs in less time

  9. Strongly typed headers • Headers are easy to mistype • The errors are hard to spot • Even big sites make mistakes • Pramga:no-cache • LastModified:Fri, 08 Mar 2013 19:26:00 GMT • Cneonction:close • ntCoent-Length:190946

  10. Host • // HostName is part of the Windows.Networking namespace. • var request = newHttpRequestMessage(); • request.Headers.Host = newHostName("contoso.com");

  11. Content-Length • ulong len = response.Content.Headers.ContentLength.Value; • byte[] buffer = newbyte[len];

  12. Last-Modified • DateTimeOffset d = response.Content.Headers.LastModified.Value;

  13. Setting a custom header • // Add a custom header to the request. • request.Headers.TryAppendWithoutValidation ("X-Requested-With", "XMLHttpRequest");

  14. List all headers • // List all the headers in the response and response.Content.foreach(var header in httpResponse.Headers) • { • Log (header.Key + "=" + header.Value); • } • foreach (var header in httpResponse.Content.Headers) • { • Log (header.Key + "=" + header.Value); • }

  15. Reason #3:list, set and delete cookies

  16. How to set a cookie • var bpf = newHttpBaseProtocolFilter(); • var cookieManager = bpf.CookieManager; • var cookie = newHttpCookie("myCookieName", ".example.com", "/"); • cookie.Value = "myValue"; • cookieManager.SetCookie(cookie); • // Use this base protocol file with an HttpClient • var httpClient = newHttpClient(bpf);

  17. Let’s see it in action!

  18. Reason #4:Influence over the system network cache

  19. Where did my data come from? • var httpResponse = awaithttpClient.GetAsync(uri); • switch(httpResponse.Source) • { • caseHttpResponseMessageSource.Cache: break; • caseHttpResponseMessageSource.Network: break; • caseHttpResponseMessageSource.None: break; • }

  20. Cache write behavior • var bpf = newHttpBaseProtocolFilter(); • // Set the WriteBehavior. • bpf.CacheControl.WriteBehavior = HttpCacheWriteBehavior.Default; • bpf.CacheControl.WriteBehavior = HttpCacheWriteBehavior.NoCache; • // Use the base protocol filter in an HttpClient • var hc = newHttpClient(bpf);

  21. Cache read behavior • var bpf = newHttpBaseProtocolFilter(); • // Set the ReadBehavior. • bpf.CacheControl.ReadBehavior = HttpCacheReadBehavior.Default; • bpf.CacheControl.ReadBehavior = HttpCacheReadBehavior.MostRecent; • bpf.CacheControl.ReadBehavior = HttpCacheReadBehavior.OnlyFromCache; • // Use the base protocol filter in an HttpClient • var hc = newHttpClient(bpf);

  22. Reason #5:The HTTP Processing pipeline includes your filter code

  23. Remember this diagram? HttpClient (has common send methods) Http Base Protocol Filter (has in-depth settings) Your App REST/Web Service HttpRequestMessage HttpResponseMessage ⛭ HttpContent String • Stream • Buffer • Multipart • FormUrlEncoded

  24. The filter pipeline HttpClient (has common send methods) 503 Retry Filter Metered Network Filter Auth Filter Http Base Protocol Filter (has in-depth settings) Your App REST/Web Service HttpRequestMessage ⛭ HttpResponseMessage HttpContent String • Stream • Buffer • Multipart • FormUrlEncoded

  25. Let’s see it in action!

  26. IHttpFilters • Filters are for: • Auth • Custom caches • Logging • Network Restrictions • Retry • Testing • Filters are WinRT Components • They implement Windows.Web.Http.Filters.IHttpFilter • It’s just one method: SendRequestAsync (+dispose) • You put them into the filter pipeline • Most pipelines end with a HttpBaseProtocolFilter • Each filter sees all requests going out • And all responses coming back • And can modify them

  27. Recap HttpClient Delete GetPost Put SendRequestAsync DefaultRequestHeaders 503 Retry Filter Metered Network Filter Auth Filter Http Base Protocol Filter REST/Web Service Your App HttpRequestMessage HttpResponseMessage HttpContent String • Stream • Buffer • Multipart • FormUrlEncoded

  28. 5 top reasons to use the new HttpClient API C++ C# JavaScript Reason new! ✔ ✔ Shared cache, cookies, credentials new! new! ✔ Strongly typed headers=fewer bugs in less time new! new! ✔ Access to cookies and shared cookies new! new! new! Control over caching and shared cache new! ✔ new! Inject your code modules into the processing pipeline=cleaner, more modular code

  29. Reasons 6 and up… • HttpProgress • SSL/TLS errors/certificates (including stronger security) • Streaming uploads & Streaming downloads • Additional settings on HttpBaseProtocolFilter • Make your own IHttpContent & IHttpFilter classes • Configuring the OAuth 2.0 filter • Server/Proxy credentials • Client certificate

  30. How to get started • HttpClient sample has lots of code and the metered network filter and the 503 retry filter • Web Authorization Broker sample has an OAuth 2.0 filter • MSDN documentation (look under Windows.Web.Http) • MSDN Windows Store apps forums

  31. Resources • 3-090: Building great service connected apps • 3-113: Building a great authentication experience into your app • //build/ 2011 • PLAT-785T: Creating connected apps that work on today’s networks • Windows app developer blog • April 10, 2013: Creating connected Windows Store apps

  32. What to do next? • Find and use filters • Make and share filters • Update your code to use the new classes

  33. Required Slide *delete this box when your slide is finalized Your MS Tag will be inserted here during the final scrub. Evaluate this session • Scan this QR codeto evaluate this session and be automatically entered in a drawing to win a prize!

More Related