5 top reasons to use the new HttpClient API Reason C++ C# JavaScript 1.Shared cache, cookies,...
-
Upload
martin-jacobs -
Category
Documents
-
view
225 -
download
0
Transcript of 5 top reasons to use the new HttpClient API Reason C++ C# JavaScript 1.Shared cache, cookies,...
![Page 1: 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.](https://reader035.fdocuments.us/reader035/viewer/2022062217/5697c00b1a28abf838cc80c2/html5/thumbnails/1.jpg)
![Page 2: 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.](https://reader035.fdocuments.us/reader035/viewer/2022062217/5697c00b1a28abf838cc80c2/html5/thumbnails/2.jpg)
Five great reasons to use the new HttpClient API
Peter SmithProgram Manager4-092
![Page 3: 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.](https://reader035.fdocuments.us/reader035/viewer/2022062217/5697c00b1a28abf838cc80c2/html5/thumbnails/3.jpg)
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: 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.](https://reader035.fdocuments.us/reader035/viewer/2022062217/5697c00b1a28abf838cc80c2/html5/thumbnails/4.jpg)
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: 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.](https://reader035.fdocuments.us/reader035/viewer/2022062217/5697c00b1a28abf838cc80c2/html5/thumbnails/5.jpg)
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)
{
}
![Page 6: 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.](https://reader035.fdocuments.us/reader035/viewer/2022062217/5697c00b1a28abf838cc80c2/html5/thumbnails/6.jpg)
Let’s see it in action!
![Page 7: 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.](https://reader035.fdocuments.us/reader035/viewer/2022062217/5697c00b1a28abf838cc80c2/html5/thumbnails/7.jpg)
Reason #1:
HTTP Integration: cache, cookies, credentials…
![Page 8: 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.](https://reader035.fdocuments.us/reader035/viewer/2022062217/5697c00b1a28abf838cc80c2/html5/thumbnails/8.jpg)
Cache, cookies, credentials are already shared between <WebView>, <Image>, <Media>, …
These shared with Windows.Web.Http, too
(but not shared across apps)
![Page 9: 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.](https://reader035.fdocuments.us/reader035/viewer/2022062217/5697c00b1a28abf838cc80c2/html5/thumbnails/9.jpg)
Reason #2:
Strongly typed headers: fewer bugs in less time
![Page 10: 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.](https://reader035.fdocuments.us/reader035/viewer/2022062217/5697c00b1a28abf838cc80c2/html5/thumbnails/10.jpg)
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: 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.](https://reader035.fdocuments.us/reader035/viewer/2022062217/5697c00b1a28abf838cc80c2/html5/thumbnails/11.jpg)
Host
// HostName is part of the Windows.Networking namespace.
var request = new HttpRequestMessage();
request.Headers.Host = new HostName("contoso.com");
![Page 12: 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.](https://reader035.fdocuments.us/reader035/viewer/2022062217/5697c00b1a28abf838cc80c2/html5/thumbnails/12.jpg)
Content-Length
ulong len = response.Content.Headers.ContentLength.Value;
byte[] buffer = new byte[len];
![Page 13: 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.](https://reader035.fdocuments.us/reader035/viewer/2022062217/5697c00b1a28abf838cc80c2/html5/thumbnails/13.jpg)
Last-Modified
DateTimeOffset d = response.Content.Headers.LastModified.Value;
![Page 14: 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.](https://reader035.fdocuments.us/reader035/viewer/2022062217/5697c00b1a28abf838cc80c2/html5/thumbnails/14.jpg)
Setting a custom header
// Add a custom header to the request.
request.Headers.TryAppendWithoutValidation("X-Requested-With", "XMLHttpRequest");
![Page 15: 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.](https://reader035.fdocuments.us/reader035/viewer/2022062217/5697c00b1a28abf838cc80c2/html5/thumbnails/15.jpg)
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: 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.](https://reader035.fdocuments.us/reader035/viewer/2022062217/5697c00b1a28abf838cc80c2/html5/thumbnails/16.jpg)
Reason #3:
list, set and delete cookies
![Page 17: 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.](https://reader035.fdocuments.us/reader035/viewer/2022062217/5697c00b1a28abf838cc80c2/html5/thumbnails/17.jpg)
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: 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.](https://reader035.fdocuments.us/reader035/viewer/2022062217/5697c00b1a28abf838cc80c2/html5/thumbnails/18.jpg)
Let’s see it in action!
![Page 19: 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.](https://reader035.fdocuments.us/reader035/viewer/2022062217/5697c00b1a28abf838cc80c2/html5/thumbnails/19.jpg)
Reason #4:
Influence over the system network cache
![Page 20: 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.](https://reader035.fdocuments.us/reader035/viewer/2022062217/5697c00b1a28abf838cc80c2/html5/thumbnails/20.jpg)
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: 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.](https://reader035.fdocuments.us/reader035/viewer/2022062217/5697c00b1a28abf838cc80c2/html5/thumbnails/21.jpg)
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 HttpClient
var hc = new HttpClient(bpf);
![Page 22: 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.](https://reader035.fdocuments.us/reader035/viewer/2022062217/5697c00b1a28abf838cc80c2/html5/thumbnails/22.jpg)
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 HttpClient
var hc = new HttpClient(bpf);
![Page 23: 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.](https://reader035.fdocuments.us/reader035/viewer/2022062217/5697c00b1a28abf838cc80c2/html5/thumbnails/23.jpg)
Reason #5:
The HTTP Processing pipeline includes your filter code
![Page 24: 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.](https://reader035.fdocuments.us/reader035/viewer/2022062217/5697c00b1a28abf838cc80c2/html5/thumbnails/24.jpg)
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: 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.](https://reader035.fdocuments.us/reader035/viewer/2022062217/5697c00b1a28abf838cc80c2/html5/thumbnails/25.jpg)
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)
HttpContentString • Stream • Buffer
• Multipart • FormUrlEncoded
⛭
![Page 26: 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.](https://reader035.fdocuments.us/reader035/viewer/2022062217/5697c00b1a28abf838cc80c2/html5/thumbnails/26.jpg)
Let’s see it in action!
![Page 27: 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.](https://reader035.fdocuments.us/reader035/viewer/2022062217/5697c00b1a28abf838cc80c2/html5/thumbnails/27.jpg)
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: 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.](https://reader035.fdocuments.us/reader035/viewer/2022062217/5697c00b1a28abf838cc80c2/html5/thumbnails/28.jpg)
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: 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.](https://reader035.fdocuments.us/reader035/viewer/2022062217/5697c00b1a28abf838cc80c2/html5/thumbnails/29.jpg)
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: 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.](https://reader035.fdocuments.us/reader035/viewer/2022062217/5697c00b1a28abf838cc80c2/html5/thumbnails/30.jpg)
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: 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.](https://reader035.fdocuments.us/reader035/viewer/2022062217/5697c00b1a28abf838cc80c2/html5/thumbnails/31.jpg)
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: 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.](https://reader035.fdocuments.us/reader035/viewer/2022062217/5697c00b1a28abf838cc80c2/html5/thumbnails/32.jpg)
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 apps
![Page 33: 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.](https://reader035.fdocuments.us/reader035/viewer/2022062217/5697c00b1a28abf838cc80c2/html5/thumbnails/33.jpg)
What to do next?
Find and use filtersMake and share filtersUpdate your code to use the new classes
![Page 34: 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.](https://reader035.fdocuments.us/reader035/viewer/2022062217/5697c00b1a28abf838cc80c2/html5/thumbnails/34.jpg)
Evaluate this session
Scan this QR code to evaluate this session and be automatically entered in a drawing to win a prize!
![Page 35: 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.](https://reader035.fdocuments.us/reader035/viewer/2022062217/5697c00b1a28abf838cc80c2/html5/thumbnails/35.jpg)
© 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.