生活随笔
收集整理的這篇文章主要介紹了
【.NET 日常开发技巧】一个性能强悍的HttpClient 库
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
微信公眾號(hào):趣編程ACE
關(guān)注可了解更多的.NET日常實(shí)戰(zhàn)開發(fā)技巧,如需源碼請(qǐng)公眾號(hào)留言源碼即可;
源碼如下:1//?See?https://aka.ms/new-console-template?for?more?information2using?Flurl;3using?Flurl.Http;45Console.WriteLine("Hello,?World!");678var?baseUrl="http://jsonplaceholder.typicode.com/";910//?http://jsonplaceholder.typicode.com/posts11var?url?=?baseUrl.AppendPathSegment("posts");12url.SetQueryParams(new?{13????api_key="some?key",14????client="test"15});1617url.SetQueryParams("test=1","value=2");1819url.SetQueryParam("paging",new?[]{1,2,3});2021//?System.Console.WriteLine(url);2223//?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);303132//?foreach?(var?(key,value)?in?url.QueryParams)33//?{34//?????System.Console.WriteLine("key:"+key+"?value:"+value);35//?}3637//?請(qǐng)求http3839//?Get使用40//?var?result?=?await?baseUrl.AppendPathSegment("posts").GetAsync();41var?result?=?await?baseUrl.AppendPathSegment("posts").SetQueryParams(new?{42????userId=143}).GetAsync();44System.Console.WriteLine(result.StatusCode);?//?2004546//?foreach?(var?(key,value)?in?result.Headers)47//?{48//??????System.Console.WriteLine("key:"+key+"?value:"+value);49//?}5051//?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("********************");585960//?//?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>();6667//?System.Console.WriteLine(resultByPost.Title);68//?System.Console.WriteLine("********************");697071//?//?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("********************");7980//?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("********************");888990//?Delete?的使用91//?var?resultByDelete?=?await?baseUrl.AppendPathSegment("posts").AppendPathSegment(1)92//?????????????????????????????????.DeleteAsync();93//?System.Console.WriteLine(resultByDelete);94//?System.Console.WriteLine("********************");9596public?class?Post97{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}
總結(jié)
以上是生活随笔為你收集整理的【.NET 日常开发技巧】一个性能强悍的HttpClient 库的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。