five great reasons to use the new httpclient api

35

Upload: wilmet

Post on 24-Feb-2016

49 views

Category:

Documents


0 download

DESCRIPTION

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!. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Five great reasons to use the new HttpClient API
Page 2: Five great reasons to use the new HttpClient API

Five great reasons to use the new HttpClient API

Peter SmithProgram Manager4-092

Page 3: Five great reasons to use the new HttpClient API

5 top reasons to use the new HttpClient API

Reason C++ C# JavaScript

1. Shared cache, cookies, credentials ✔2. Strongly typed headers=fewer bugs in less time

3. Access to cookies and shared cookies

4. Control over caching and shared cache

5. Inject your code modules into the processing pipeline=cleaner, more modular code

✔new!

✔ new! new!

new! ✔ new!

new! ✔ new!

new! new! new!

Page 4: Five great reasons to use the new HttpClient API

REST/Web Service

HttpClient(has common

send methods)

Meet the family!Your App

HttpRequestMessage

HttpResponseMessage

Http Base Protocol

Filter(has in-depth

settings)

HttpContentString • Stream • Buffer

• Multipart • FormUrlEncoded

Page 5: Five great reasons to use the new HttpClient API

Simple exampletry{ var uri = new Uri("http://example.com/datalist.aspx"); var httpClient = new HttpClient(); var result = await httpClient.GetStringAsync(uri);}catch (Exception e){}

Page 6: Five great reasons to use the new HttpClient API

Let’s see it in action!

Page 7: Five great reasons to use the new HttpClient API

Reason #1:

HTTP Integration: cache, cookies, credentials…

Page 8: Five great reasons to use the new HttpClient API

Cache, cookies, credentials are already shared between <WebView>, <Image>, <Media>, …

These shared with Windows.Web.Http, too

(but not shared across apps)

Page 9: Five great reasons to use the new HttpClient API

Reason #2:

Strongly typed headers: fewer bugs in less time

Page 10: Five great reasons to use the new HttpClient API

Pramga:no-cacheLastModified:Fri, 08 Mar 2013 19:26:00 GMTCneonction:closentCoent-Length:190946

Headers are easy to mistype

The errors are hard to spot

Even big sites make mistakes

Strongly typed headers

Page 11: Five great reasons to use the new HttpClient API

Host// HostName is part of the Windows.Networking namespace.var request = new HttpRequestMessage();request.Headers.Host = new HostName("contoso.com");

Page 12: Five great reasons to use the new HttpClient API

Content-Lengthulong len = response.Content.Headers.ContentLength.Value;byte[] buffer = new byte[len];

Page 13: Five great reasons to use the new HttpClient API

Last-Modified

DateTimeOffset d = response.Content.Headers.LastModified.Value;

Page 14: Five great reasons to use the new HttpClient API

Setting a custom header

// Add a custom header to the request.request.Headers.TryAppendWithoutValidation

("X-Requested-With", "XMLHttpRequest");

Page 15: Five great reasons to use the new HttpClient API

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); }

Page 16: Five great reasons to use the new HttpClient API

Reason #3:

list, set and delete cookies

Page 17: Five great reasons to use the new HttpClient API

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

Page 18: Five great reasons to use the new HttpClient API

Let’s see it in action!

Page 19: Five great reasons to use the new HttpClient API

Reason #4:

Influence over the system network cache

Page 20: Five great reasons to use the new HttpClient API

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; }

Page 21: Five great reasons to use the new HttpClient API

Cache write behaviorvar bpf = new HttpBaseProtocolFilter();

// Set the WriteBehavior.bpf.CacheControl.WriteBehavior = HttpCacheWriteBehavior.Default;bpf.CacheControl.WriteBehavior = HttpCacheWriteBehavior.NoCache;

// Use the base protocol filter in an HttpClientvar hc = new HttpClient(bpf);

Page 22: Five great reasons to use the new HttpClient API

Cache read behaviorvar 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 HttpClientvar hc = new HttpClient(bpf);

Page 23: Five great reasons to use the new HttpClient API

Reason #5:

The HTTP Processing pipeline includes your filter code

Page 24: Five great reasons to use the new HttpClient API

REST/Web Service

HttpClient(has common

send methods)

Remember this diagram?

Your App

HttpRequestMessage

HttpResponseMessage

Http Base Protocol

Filter(has in-depth

settings)

HttpContentString • Stream • Buffer

• Multipart • FormUrlEncoded

Page 25: Five great reasons to use the new HttpClient API

Auth Filter

503 Retry Filter

Metered Network Filter

REST/Web Service

HttpClient(has common

send methods)

The filter pipeline

Your App

HttpRequestMessageHttpResponseMess

age

Http Base Protocol

Filter(has in-depth

settings)

HttpContentString • Stream • Buffer

• Multipart • FormUrlEncoded

Page 26: Five great reasons to use the new HttpClient API

Let’s see it in action!

Page 27: Five great reasons to use the new HttpClient API

Filters are WinRT ComponentsThey implement Windows.Web.Http.Filters.IHttpFilterIt’s just one method: SendRequestAsync (+dispose)You put them into the filter pipelineMost pipelines end with a HttpBaseProtocolFilterEach filter sees all requests going outAnd all responses coming backAnd can modify them

Filters are for:AuthCustom cachesLoggingNetwork RestrictionsRetryTesting

IHttpFilters

Page 28: Five great reasons to use the new HttpClient API

REST/Web Service

Auth Filter

503 Retry Filter

Metered Network Filter

Recap

HttpClientDelete Get Post Put SendRequestAsync

DefaultRequestHeaders

Your App

HttpRequestMessage

Http Base

Protocol Filter

HttpContentString • Stream • Buffer

• Multipart • FormUrlEncoded

HttpResponseMessage

Page 29: Five great reasons to use the new HttpClient API

5 top reasons to use the new HttpClient API

Reason C++ C# JavaScript

1. Shared cache, cookies, credentials ✔2. Strongly typed headers=fewer bugs in less time

3. Access to cookies and shared cookies

4. Control over caching and shared cache

5. Inject your code modules into the processing pipeline=cleaner, more modular code

✔new!

✔ new! new!

new! ✔ new!

new! ✔ new!

new! new! new!

Page 30: Five great reasons to use the new HttpClient API

HttpProgressSSL/TLS errors/certificates (including stronger security)Streaming uploads & Streaming downloadsAdditional settings on HttpBaseProtocolFilterMake your own IHttpContent & IHttpFilter classesConfiguring the OAuth 2.0 filterServer/Proxy credentialsClient certificate

Reasons 6 and up…

Page 31: Five great reasons to use the new HttpClient API

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

How to get started

Page 32: Five great reasons to use the new HttpClient API

Resources3-090: Building great service connected apps3-113: Building a great authentication experience into your app

//build/ 2011PLAT-785T: Creating connected apps that work on today’s networks

Windows app developer blogApril 10, 2013: Creating connected Windows Store apps

Page 33: Five great reasons to use the new HttpClient API

What to do next?Find and use filtersMake and share filtersUpdate your code to use the new classes

Page 34: Five great reasons to use the new HttpClient API

Evaluate this session

Scan this QR code to evaluate this session and be automatically entered in a drawing to win a prize!

Page 35: Five great reasons to use the new HttpClient API

© 2013 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.