Presentation is loading. Please wait.

Presentation is loading. Please wait.

Five great reasons to use the new HttpClient API

Similar presentations


Presentation on theme: "Five great reasons to use the new HttpClient API"— Presentation transcript:

1

2 Five great reasons to use the new HttpClient API
Windows Azure 4/26/2017 Five great reasons to use the new HttpClient API Peter Smith Program Manager 4-092 © 2011 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

3 5 top reasons to use the new HttpClient API
Build 2012 4/26/2017 5 top reasons to use the new HttpClient API Reason C++ C# JavaScript Shared cache, cookies, credentials new! 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 © 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

4 Meet the family! ⛭ HttpClient Http Base Protocol Filter Your App
Build 2012 4/26/2017 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 © 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

5 Build 2012 4/26/2017 Simple example try { var uri = new Uri(" var httpClient = new HttpClient(); var result = await httpClient.GetStringAsync(uri); } catch (Exception e) © 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

6 Let’s see it in action! Build 2012 4/26/2017
© 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

7 Reason #1: HTTP Integration: cache, cookies, credentials…
Build 2012 4/26/2017 Reason #1: HTTP Integration: cache, cookies, credentials… © 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

8 Build 2012 4/26/2017 Cache, cookies, credentials are already shared between <WebView>, <Image>, <Media>, … These shared with Windows.Web.Http, too (but not shared across apps) © 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

9 Reason #2: Strongly typed headers: fewer bugs in less time
Build 2012 4/26/2017 Reason #2: Strongly typed headers: fewer bugs in less time © 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

10 Strongly typed headers
Build 2012 4/26/2017 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 :26:00 GMT Cneonction:close ntCoent-Length:190946 © 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

11 Build 2012 4/26/2017 Host // HostName is part of the Windows.Networking namespace. var request = new HttpRequestMessage(); request.Headers.Host = new HostName("contoso.com"); © 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

12 Build 2012 4/26/2017 Content-Length ulong len = response.Content.Headers.ContentLength.Value; byte[] buffer = new byte[len]; © 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

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

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

15 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)

16 Reason #3: list, set and delete cookies
Build 2012 4/26/2017 Reason #3: list, set and delete cookies © 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

17 Build 2012 4/26/2017 How to set a cookie var bpf = new HttpBaseProtocolFilter(); var cookieManager = bpf.CookieManager; var cookie = new HttpCookie("myCookieName", ".example.com", "/"); cookie.Value = "myValue"; cookieManager.SetCookie(cookie); // Use this base protocol file with an HttpClient var httpClient = new HttpClient(bpf); © 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

18 Let’s see it in action!

19 Reason #4: Influence over the system network cache
Build 2012 4/26/2017 Reason #4: Influence over the system network cache © 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

20 Where did my data come from?
var httpResponse = await httpClient.GetAsync(uri); switch (httpResponse.Source) { case HttpResponseMessageSource.Cache: break; case HttpResponseMessageSource.Network: break; case HttpResponseMessageSource.None: break; }

21 Cache write behavior var bpf = new HttpBaseProtocolFilter(); // Set the WriteBehavior. bpf.CacheControl.WriteBehavior = HttpCacheWriteBehavior.Default; bpf.CacheControl.WriteBehavior = HttpCacheWriteBehavior.NoCache; // Use the base protocol filter in an HttpClient var hc = new HttpClient(bpf);

22 Build 2012 4/26/2017 Cache read behavior var bpf = new HttpBaseProtocolFilter(); // 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 = new HttpClient(bpf); © 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

23 Reason #5: The HTTP Processing pipeline includes your filter code
Build 2012 4/26/2017 Reason #5: The HTTP Processing pipeline includes your filter code © 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

24 Remember this diagram? ⛭ HttpClient Http Base Protocol Filter Your App
Build 2012 4/26/2017 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 © 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

25 The filter pipeline ⛭ HttpClient Http Base Protocol Filter Your App
Build 2012 4/26/2017 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 © 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

26 Let’s see it in action! Build 2012 4/26/2017
© 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

27 IHttpFilters Filters are WinRT Components
Build 2012 4/26/2017 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 © 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

28 Recap HttpClient Your App Http Base Protocol Filter
Build 2012 4/26/2017 Recap HttpClient Delete Get Post Put SendRequestAsync DefaultRequestHeaders 503 Retry Filter Metered Network Filter Auth Filter Http Base Protocol Filter Your App REST/Web Service HttpRequestMessage HttpResponseMessage HttpContent String • Stream • Buffer • Multipart • FormUrlEncoded © 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

29 5 top reasons to use the new HttpClient API
Build 2012 4/26/2017 5 top reasons to use the new HttpClient API Reason C++ C# JavaScript Shared cache, cookies, credentials new! 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 © 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

30 Reasons 6 and up… HttpProgress
Build 2012 4/26/2017 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 © 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

31 Build 2012 4/26/2017 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 © 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

32 Resources 3-090: Building great service connected apps
4/26/2017 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 © 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

33 What to do next? Find and use filters Make and share filters
Build 2012 4/26/2017 What to do next? Find and use filters Make and share filters Update your code to use the new classes © 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

34 4/26/2017 5:02 AM 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 code to evaluate this session and be automatically entered in a drawing to win a prize! © 2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

35


Download ppt "Five great reasons to use the new HttpClient API"

Similar presentations


Ads by Google