Dapplo.HttpExtensions
Sometimes you just want to access a service in the internet or on a local server. This project helps you to deal with a lot of difficult stuff, like having a default proxy or even making a specify proxy possible. Also it can handle Json communication, which a lot of REST based APIs use.
A short example:
using Dapplo.HttpExtensions;
var uri = new Uri("http://myserver/");
var response = await uri.GetAsAsync<string>();
A more "complex" example of how you can use this, is available in the test.
There is a test with calling a JSON Service (GitHub): UriActionExtensionsTests.TestGetAsJsonAsync_GitHubApiReleases.
There is also an OAuth test: OAuth/OAuthTests.TestOAuthHttpMessageHandler. This is not running during the build, as it needs "human" interaction with a browser.
Some of the features:
- Fluent API
- Progress support, for uploading and downloading
- Typed access to Http content (e.g. GetAsAsync
) - Typed upload
- Json support, in two variants:
- SimpleJson via nuget package Dapplo.HttpExtensions.JsonSimple and call SimpleJsonSerializer.RegisterGlobally() or set IHttpBehavior.JsonSerializer to new SimpleJsonSerializer();
- install nuget package Dapplo.HttpExtensions.JsonNet and call JsonNetJsonSerializer.RegisterGlobally(); or set IHttpBehavior.JsonSerializer to new JsonNetJsonSerializer();
- OAuth 1 & 2 via nuget package Dapplo.HttpExtensions.OAuth, this is currently work in process but with some servers it should already be usable.
Notes: This project uses async code, and tries to conform to the Task-bases Asynchronous Pattern (TAP). Just so you know why sometimes the method name look odd... Meaning all async methods have names which end with Async and (where possible) accept a CancellationToken. This is the final parameter, as adviced here: https://blogs.msdn.microsoft.com/andrewarnottms/2014/03/19/recommended-patterns-for-cancellationtoken/
API Changes, with every 0.x change the signatures have changed:
In 0.2.x a HttpBehaviour object was added to prevent future signature changes as much as possible.
In 0.3.x a lot of previous method were combined. (GetAsMemoryStream -> GetAsAsync