/
Five great reasons to use the new HttpClient API Five great reasons to use the new HttpClient API

Five great reasons to use the new HttpClient API - PowerPoint Presentation

luanne-stotts
luanne-stotts . @luanne-stotts
Follow
422 views
Uploaded On 2016-06-01

Five great reasons to use the new HttpClient API - PPT Presentation

Peter Smith Program Manager 4092 5 top reasons to use the new HttpClient API Reason C C JavaScript Shared cache cookies credentials Strongly typed headersfewer bugs in less time ID: 344701

filter httpclient var headers httpclient filter headers var bpf cache http shared cookies code header reason base protocol web

Share:

Link:

Embed:

Download Presentation from below link

Download Presentation The PPT/PDF document "Five great reasons to use the new HttpCl..." is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

Slide1
Slide2

Five great reasons to use the new HttpClient API

Peter Smith

Program Manager

4-092Slide3

5 top reasons to use the new HttpClient API

Reason

C++

C#

JavaScript

Shared

cache, cookies,

credentials

Strongly typed headers=fewer bugs in less time

Access to cookies and shared cookies

Control over caching and shared cache

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

new!

new!

new!

new!

new!

new!

new!

new!

new!

new!Slide4

REST/Web Service

HttpClient

(has common send methods)

Meet the family!

Your App

HttpRequestMessage

HttpResponseMessage

Http Base Protocol

Filter

(has in-depth settings)

HttpContent

String

Stream

• Buffer • Multipart

• FormUrlEncoded⛭Slide5

Simple example

try

{

var

uri = new Uri("http://example.com/datalist.aspx"

); var httpClient = new

HttpClient(); var

result = await httpClient.GetStringAsync(uri);}catch

(Exception e){}Slide6

Let’s see it in action!Slide7

Reason #1:

HTTP Integration: cache, cookies, credentials…Slide8

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

These shared with

Windows.Web.Http

, too(but not shared across apps)Slide9

Reason #2:

Strongly typed headers: fewer bugs in less timeSlide10

Pramga:no-cacheLastModified:Fri, 08 Mar 2013 19:26:00 GMT

Cneonction:close

ntCoent-Length:190946

Headers are easy to mistypeThe errors are hard to spot

Even big sites make mistakesStrongly typed headersSlide11

Host

// HostName is part of the Windows.Networking namespace.

var

request = new

HttpRequestMessage();request.Headers.Host = new

HostName("contoso.com");Slide12

Content-Length

ulong

len = response.Content.Headers.ContentLength.Value;

byte[] buffer = new

byte[len];Slide13

Last-Modified

DateTimeOffset

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

Setting a custom header

// Add a custom header to the request.

request.Headers.TryAppendWithoutValidation

(

"X-Requested-With", "XMLHttpRequest"

);Slide15

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

}Slide16

Reason #3:

list, set and delete cookiesSlide17

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

Let’s see it in action!Slide19

Reason #4:

Influence over the system network cache Slide20

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

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

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 HttpClientvar hc = new

HttpClient(bpf);Slide23

Reason #5:

The HTTP Processing pipeline includes your filter codeSlide24

REST/Web Service

HttpClient

(has common send methods)

Remember this diagram?

Your App

HttpRequestMessage

HttpResponseMessage

Http Base Protocol

Filter

(has in-depth settings)

HttpContent

String

Stream

• Buffer • Multipart

• FormUrlEncoded⛭Slide25

Auth

Filter

503 Retry Filter

Metered Network Filter

REST/Web Service

HttpClient

(has common send methods)

The filter pipeline

Your App

HttpRequestMessage

HttpResponseMessage

Http Base Protocol

Filter

(has in-depth settings)

HttpContent

String • Stream

• Buffer • Multipart • FormUrlEncoded⛭Slide26

Let’s see it in action!Slide27

Filters are WinRT ComponentsThey 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 HttpBaseProtocolFilterEach filter sees all requests going outAnd all responses coming backAnd can modify them

Filters are for:AuthCustom cachesLoggingNetwork RestrictionsRetryTesting

IHttpFiltersSlide28

REST/Web Service

Auth

Filter

503 Retry Filter

Metered Network Filter

Recap

HttpClient

Delete Get

Post Put SendRequestAsync

DefaultRequestHeaders

Your App

HttpRequestMessage

Http Base Protocol Filter

HttpContent

String • Stream • Buffer • Multipart • FormUrlEncoded

HttpResponseMessageSlide29

5 top reasons to use the new HttpClient API

Reason

C++

C#

JavaScript

Shared

cache, cookies,

credentials

Strongly typed headers=fewer bugs in less time

Access to cookies and shared cookies

Control over caching and shared cache

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

new!

new!

new!

new!

new!

new!

new!

new!

new!

new!Slide30

HttpProgressSSL/TLS errors/certificates (including stronger security)

Streaming uploads & Streaming downloads

Additional settings on HttpBaseProtocolFilter

Make your own IHttpContent & IHttpFilter classesConfiguring the OAuth 2.0 filterServer/Proxy credentials

Client certificateReasons 6 and up…Slide31

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 startedSlide32

Resources

3-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 appsSlide33

What to do next?

Find and use filters

Make and share filters

Update your code to use the new classesSlide34

Evaluate this session

Scan this QR code

to evaluate this session and be automatically entered in a

drawing

to

win a prize!

Required Slide

*delete this box when your slide is finalized

Your MS Tag will be inserted here during the final scrub. Slide35