C#   发布时间:2022-04-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了使用 HttpRequester 更方便的发起 HTTP 请求大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

使用 httprequester 更方便的发起 http 请求

Intro

一直感觉 .net 里面(这里主要说的是 .net framework 下)发送 http 请求的方式用着不是特别好用,而且在 .net framework 里发送 http 请求的方式有好几种,如:WebClient/Webrequest/httpClient,于是自己封装了一个 httprequester

WebClient 主要是用来下载,不能对 http 做较多的自定义httpClient 是微软后来加入的,也是比较推荐使用的处理 http 请求的,但是在 .net framework 下如果不注意的话可能会造成很大的灾难,从 .net core 2.1 开始,微软引入了 httpClientFactory解决了一些问题,如果你是在 .net core 程序下跑的话,推荐使用 httpClient,如果在 .net framework 下跑的话可以使用 Webrequest这里说明一下,.net core 下,Webrequest 内部也是基于 httpClient 的,详细可以参 https://github.com/dotnet/corefx/blob/master/src/System.Net.Requests/src/System/Net/HttpWebRequest.cs#L1096

使用 HttpRequester 更方便的发起 HTTP 请求

httprequester 是基于 Webrequest 封装的,使用比较简洁的 Fluent API 的方式调用,如果是在 .net framework 下开发,可以尝试使用一下,具体使用可以参下面的示例以及 Github 上的示例代码 示例代码2

添加 Nuget 包引用

添加WeihanLi.Common 的引用,需要 1.0.14 及以上版本

使用 httprequester


var result = new httprequester("https://weihanli.xyz") // 使用 GET 方式请求 https://weihanli.xyz
                .Execute(); // 返回 responseText
System.Console.WriteLine(result);

 // 使用 POST 方法请求 https://accounTing.weihanli.xyz/Account/logon
var loginResult = new httprequester("https://accounTing.weihanli.xyz/Account/logon",httpR_68_11845@ethod.Post)
                .WithHeaders(new Dictionary<String,String>()
                {
                    { "X-requested-With","XMLhttprequest" },}) // 设置请求头
                // .Ajaxrequest(true)
                // 设置 Referer,在做爬虫时会比较有用,还可以通过 WithProxy("proxyUrl") 设置代理
                .WithReferer("https://accounTing.weihanli.xyz/Account/Login?ReturnUrl=%2F") 
                // 手动设置 UserAgent,认会随机设置一个 UA
                .WithUserAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML,like Gecko) Chrome/72.0.3626.121 Safari/537.36")
                .WithFormParameters(new Dictionary<String,String>()
                {
                    {"Username","liweihan" },{"password","112233" },{"RememberMe","false" }
                }) // 设置 post 的 form 参数
                // 获取返回的 responseText,并 json 反序列化为一个强类型的Model
               .Execute<WeihanLi.Common.Models.JsonResultModel<bool>>(); 

            System.Console.WriteLine(loginResult.ToJson());

// 上传文件示例
var uploadFileResponse = new httprequester("https://graph.baidu.com/upload",httpR_68_11845@ethod.Post)
                .WithFile($@"{System.Environment.GetEnvironmentVariable("USERPROFILE")}\Pictures\4e6ab53e383863ed4d15252039f70423.jpg","image",new Dictionary<String,String>()
                {
                    { "tn","pc" },{ "from",{ "image_source","PC_UPLOAD_SEARCH_FILE" },{ "range","{\"page_from\": \"searchIndex\"}" },}) // 设置上传文件,并设置其它 form 参数信息
                .WithReferer("https://baidu.com/") // 设置 referer
                .WithUserAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML,like Gecko) Chrome/72.0.3626.121 Safari/537.36")
                .ExecuteForResponse(); // 获取一个 httpWebResponse 对象,可以使用 StatusCode/ ResponseHeader 等信息
            System.Console.WriteLine($"Response status:{uploadFileResponse.StatusCodE},result:{uploadFileResponse.ReadToEnd()}");
@H_767_55@more

除了 Header/Referer/UserAgent 之外,还可以设置 Proxy,设置 Cookie,Ajax 请求 等信息,而且还可以直接 PostJson 示例如下:

new httprequester("requesturl",httpR_68_11845@ethod.Post)
  .WithProxy("proxyUrl") // 使用代理 //.WithProxy("url","userName","password") // 配置带密码的代理
  .WithCookie(cookiE) //带 Cookie 访问 //.WithCookie("url",cookiE) // 只用指定路径的 cookie
  .WithJsonParameter(entity) // post 一个 json 对象,content-type 会自动设置为 `application/json`
  .Ajaxrequest(true) // 设置该请求是 Ajax 请求
  .Execute();
@H_802_63@memo

欢迎体验~,如果有什么问题或者发现什么 bug 欢迎和我联系或者给我提 issue

大佬总结

以上是大佬教程为你收集整理的使用 HttpRequester 更方便的发起 HTTP 请求全部内容,希望文章能够帮你解决使用 HttpRequester 更方便的发起 HTTP 请求所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: