How to efficiently handle Http requests using Flurl in. NET

  • 2021-11-29 23:36:16
  • OfStack

Directory Introduction Url Construction Http Enhancement HttpClient management Summarize

Brief introduction

Officially, Flurl is a modern, fluent, asynchronous, testable, portable, URL enhanced and Http client component.

Url Construction

There is now a login interface with the following address:


https://www.some-api.com/login?name=Lee&pwd=123456

When we deal with this address, we will splice login, and then splice it? Number, and then splice parameters, and splice in the middle & Get the final address.

To build with Flurl, you first need to install Flurl components through Nuget.


 var url = "http://www.some-api.com"
           .AppendPathSegment("login")
           .SetQueryParams(new
           {
               name = "Lee",
               pwd = "123456" 
           });  

This is very simple, this is the simplest Get request, and we can also use the extension method of Uri


var url = new Uri("http://www.some-api.com").AppendPathSegment(...

Http Enhancement

Flurl is modular, so you also need to install Flurl. Http


using Flurl;
using Flurl.Http;

var result = await "http://www.some-api.com".AppendPathSegment("login").GetAsync();

The above code will send an GET request and return an IFlurlResponse, which can get StatusCode, Headers, etc., and can also get response content through GetStringAsync and GetJsonAsync.

If you just want to get the response content, let's see how simple Flurl is:


T poco = await "http://api.foo.com".GetJsonAsync<T>();
string text = await "http://site.com/readme.txt".GetStringAsync();
byte[] bytes = await "http://site.com/image.jpg".GetBytesAsync();
Stream stream = await "http://site.com/music.mp3".GetStreamAsync();

Post Commit


await "http://api.foo.com".PostJsonAsync(new { a = 1, b = 2 });

Dynamic type dynamic


dynamic d = await "http://api.foo.com".GetJsonAsync();

Set the request header:


await url.WithHeader("Accept", "text/plain").GetJsonAsync();

await url.WithHeaders(new { Accept = "text/plain", User_Agent = "Flurl" }).GetJsonAsync();

Basic authentication


await url.WithBasicAuth("username", "password").GetJsonAsync();

OAuth 2.0


await url.WithOAuthBearerToken("mytoken").GetJsonAsync();

Form submission


 var url = "http://www.some-api.com"
           .AppendPathSegment("login")
           .SetQueryParams(new
           {
               name = "Lee",
               pwd = "123456" 
           });  
0

HttpClient management

We usually don't create too many HttpClient, too many connections will deplete server resources, and SocketException exceptions will usually be thrown, and most of them still use HttpClientFactory.

In the Flurl repository, it is an internally managed HttpClient instance, typically a host Host, which creates an HttpClient and caches it for reuse.

Flurl also supports the IOC container well, and you can also use it in dependency injection.

Summarize

The Flurl component makes the Http operation easier to use, you can try to use it in the project, and there are 1 other functions, can be tested and configured, etc. You can find its documentation in official website

The above is how to use Flurl to efficiently handle Http requests in. NET. For more information about using Flurl to handle Http requests in. NET, please pay attention to other related articles on this site!


Related articles: