.NET 日常开发技巧一个性能强悍的HttpClient 库
Posted dotNET跨平台
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了.NET 日常开发技巧一个性能强悍的HttpClient 库相关的知识,希望对你有一定的参考价值。
微信公众号:趣编程ACE
关注可了解更多的.NET日常实战开发技巧,如需源码请公众号留言源码即可;
源码如下:
1// See https://aka.ms/new-console-template for more information
2using Flurl;
3using Flurl.Http;
4
5Console.WriteLine("Hello, World!");
6
7
8var baseUrl="http://jsonplaceholder.typicode.com/";
9
10// http://jsonplaceholder.typicode.com/posts
11var url = baseUrl.AppendPathSegment("posts");
12url.SetQueryParams(new
13 api_key="some key",
14 client="test"
15);
16
17url.SetQueryParams("test=1","value=2");
18
19url.SetQueryParam("paging",new []1,2,3);
20
21// System.Console.WriteLine(url);
22
23// System.Console.WriteLine(url);
24// System.Console.WriteLine("Scheme:"+url.Scheme);
25// System.Console.WriteLine("Host:"+url.Host);
26// System.Console.WriteLine("Port:"+url.Port);
27// System.Console.WriteLine("Query:"+url.Query);
28// System.Console.WriteLine("isRelative:"+url.IsRelative);
29// System.Console.WriteLine("isSecureScheme:"+url.IsSecureScheme);
30
31
32// foreach (var (key,value) in url.QueryParams)
33//
34// System.Console.WriteLine("key:"+key+" value:"+value);
35//
36
37// 请求http
38
39// Get使用
40// var result = await baseUrl.AppendPathSegment("posts").GetAsync();
41var result = await baseUrl.AppendPathSegment("posts").SetQueryParams(new
42 userId=1
43).GetAsync();
44System.Console.WriteLine(result.StatusCode); // 200
45
46// foreach (var (key,value) in result.Headers)
47//
48// System.Console.WriteLine("key:"+key+" value:"+value);
49//
50
51// System.Console.WriteLine( await result.GetStringAsync());
52// var post= await result.GetJsonAsync<IEnumerable<Post>>();
53// foreach (var item in post)
54//
55// System.Console.WriteLine(item.Title +" _ "+ item.Id);
56//
57// System.Console.WriteLine("********************");
58
59
60// // Post 的使用
61// var resultByPost =await baseUrl.AppendPathSegment("posts")
62// .WithHeader("Content-type","application/json")
63// .WithHeader("charset","UTF-8")
64// .PostJsonAsync(new Post()Title="foo",Body="Bar",UserId=1)
65// .ReceiveJson<Post>();
66
67// System.Console.WriteLine(resultByPost.Title);
68// System.Console.WriteLine("********************");
69
70
71// // Put的使用
72// var resultByPut = await baseUrl.AppendPathSegment("posts").AppendPathSegment(1)
73// .WithHeader("Content-type","application/json")
74// .WithHeader("charset","UTF-8")
75// .PutJsonAsync(new Post()Id=1,Title="Foo",Body="Bar",UserId=1)
76// .ReceiveJson<Post>();;
77// System.Console.WriteLine(resultByPut.Title);
78// System.Console.WriteLine("********************");
79
80// Patch 的使用
81// var resultByPatch = await baseUrl.AppendPathSegment("posts").AppendPathSegment(1)
82// .WithHeader("Content-type","application/json")
83// .WithHeader("charset","UTF-8")
84// .PatchJsonAsync(new Post()Title="FooByPathc")
85// .ReceiveJson<Post>();;
86// System.Console.WriteLine(resultByPatch.Title);
87// System.Console.WriteLine("********************");
88
89
90// Delete 的使用
91// var resultByDelete = await baseUrl.AppendPathSegment("posts").AppendPathSegment(1)
92// .DeleteAsync();
93// System.Console.WriteLine(resultByDelete);
94// System.Console.WriteLine("********************");
95
96public class Post
97
98 public int Id get; set;
99 public int UserId get; set;
100 public string? Title get; set;
101 public string? Body get; set;
102
以上是关于.NET 日常开发技巧一个性能强悍的HttpClient 库的主要内容,如果未能解决你的问题,请参考以下文章
C#/.NET 日常开发技巧JWT+ActionFilter 简便控制器代码