欢迎光临
我的个人博客网站

分享一个自己封装且一直在维护的依赖.net4.5的http异步组包工具类(支持get,post( 表单 ,json, 包含图片等文件的流提交) ,cookie管理,自动跳转,代理IP,https的支持,高并发的配置等等

1.)Nuget安装:

   搜索 ConfigLab.Comp, 安装最新版即可.

2.)组包示例.

  2.1)模拟post表单提交并包含普通参数和一个图片文件(基于HttpFileUploadAssisterAsync这个核心类).

 private async Task<string> GetPostResultForFile_Async()         {             string sRspResult = "";             IWebProxy eWebProxy = this.getWebProxy();//代理IP的设置             Dictionary<string, string> dictHeader = new Dictionary<string, string>();             string sUserAgent = this.tbxUserAgent.Text.Trim();             dictHeader["Accept-Encoding"] = "gzip, deflate";             dictHeader["Accept-Language"] = "zh-CN,zh;q=0.9";             dictHeader["Upgrade-Insecure-Requests"] = "1";             dictHeader["Cache-Control"] = "max-age=0";             dictHeader["Origin"] = "null";             Dictionary<string, string> dictImg = new Dictionary<string, string>();//待和参数一起提交的文件信息, key=FullFileName, value=Post内容中的参数Name名的字典             if (this.tbxImgUrl.Text.IndexOf(":") > -1 && this.tbxImgName.Text.Trim().Length > 0)             {                 dictImg[this.tbxImgUrl.Text] = this.tbxImgName.Text.Trim();             }             else             {                 if (this.tbxImgName.Text.Trim().Length > 0)                 {                     dictImg[""] = this.tbxImgName.Text.Trim();                 }             }             if (!string.IsNullOrEmpty(this.tbx_Hedaer_Key.Text) && !string.IsNullOrEmpty(this.tbx_Hedaer_Value.Text))             {                 dictHeader[this.tbx_Hedaer_Key.Text.Trim()] = this.tbx_Hedaer_Value.Text.Trim();             }             string sFile_ContentType = "image/jpeg";             ResponseResult result = new ResponseResult();             string sUrl = this.tbxUrl.Text.Trim();//输入的请求地址             string sPostData = this.tbxPostData.Text.Trim();//同步提交的文本参数 : u=1&a=2这种格式             string sAccess = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3";             //异步模式             HttpFileUploadAssisterAsync htool = new HttpFileUploadAssisterAsync();//关键的类             Task<ResponseResult> tsk_rrs = htool.SendRequest(new RequestParamsWithFile()             {                 URL = sUrl,                 Method = "POST",                 Timeout = 10,                 PostData = sPostData,                 DictFileName2PostName = dictImg,//可同时传递多个文件                 WriteEnc = Encoding.UTF8,                 ReaderEnc = GetEncoding(),                 WithCookie = this.btnWithCookie.Checked,//是否自动维护cookie                 DictRequestHeaderKeyValues = dictHeader,//自定义包头                 Accept = sAccess,                 UserAgent = sUserAgent,                 KeepAlive = true,                 EnableExpect100Continue = false,                 File_ContentType = sFile_ContentType,//传输的文件类型                  WebProxy = eWebProxy//代理IP             });             await tsk_rrs;             ResponseResult rrs_rspData = tsk_rrs.Result;             sRspResult = rrs_rspData.ResponseData;             return sRspResult;         }

  2.2)模拟post表单提交并包含普通参数(基于HttpClientAssisterAsync这个核心类).

        private async Task<string>  GetPostResult_Async()         {             IWebProxy eProxy = this.getWebProxy();             HttpClientAssisterAsync htool = new HttpClientAssisterAsync();             Dictionary<string, string> dictHeader = new Dictionary<string, string>();             string sUserAgent = this.tbxUserAgent.Text.Trim();             dictHeader["Content-Type"] = "application/x-www-form-urlencoded";             dictHeader["Accept-Language"] = "zh-cn";             if (!string.IsNullOrEmpty(this.tbx_Hedaer_Key.Text) && !string.IsNullOrEmpty(this.tbx_Hedaer_Value.Text))             {                 dictHeader[this.tbx_Hedaer_Key.Text.Trim()] = this.tbx_Hedaer_Value.Text.Trim();             }             Task<ResponseResult>  tsk_result = htool.SendRequest(new RequestParams() {                 URL = this.tbxUrl.Text.Trim(),                 Method = "POST",                 Timeout = 30, //单位秒                 PostData = this.tbxPostData.Text,//表单数据,u=1&p=2这种                 WriteEnc = Encoding.UTF8,                 ReaderEnc = GetEncoding(),                 WithCookie = this.btnWithCookie.Checked,//是否自动管理cookie                 DictRequestHeaderKeyValues = dictHeader, //自定义的包头信息                 Accept = "*/*",                 UserAgent = sUserAgent,                 KeepAlive = false,                 EnableP3P = false,                 AllowRedrect=false,//是否允许自动跳转                  EnableExpect100Continue=true,                 WebProxy = eProxy  //代理IP             });             await tsk_result;             ResponseResult result = tsk_result.Result;             return string.Format("optime={0}rnhttpstatucode={1}rnerror={2}rnresult={3}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), result.CurrHttpStatuCode, result.ExceptionMsg, result.ResponseData);         }

   2.3)模拟post提交一个json数据并包含普通参数(基于HttpClientAssisterAsync这个核心类).

        private async Task<string> getResultForPostJson_Async()         {             IWebProxy eProxy = this.getWebProxy();             HttpClientAssisterAsync htool = new HttpClientAssisterAsync();             Dictionary<string, string> dictHeader = new Dictionary<string, string>();             string sUserAgent = this.tbxUserAgent.Text.Trim();             dictHeader["Content-Type"] = "application/json;charset=UTF-8";//重点!!!!             dictHeader["Accept-Language"] = "zh-cn";             if (!string.IsNullOrEmpty(this.tbx_Hedaer_Key.Text) && !string.IsNullOrEmpty(this.tbx_Hedaer_Value.Text))             {                 dictHeader[this.tbx_Hedaer_Key.Text.Trim()] = this.tbx_Hedaer_Value.Text.Trim();             }             Task<ResponseResult> tsk_result = htool.SendRequest(new RequestParams()             {                 URL = this.tbxUrl.Text.Trim(),                 Method = "POST",                 Timeout = 30,                 PostData = this.tbxPostData.Text,//Json数据                 WriteEnc = Encoding.UTF8,                 ReaderEnc = GetEncoding(),                 WithCookie = this.btnWithCookie.Checked,//自动管理cookie                 DictRequestHeaderKeyValues = dictHeader,//自定义包头
Accept
= "*/*", UserAgent = sUserAgent, KeepAlive = false, EnableP3P = false, AllowRedrect = false, EnableExpect100Continue = true, WebProxy = eProxy }); await tsk_result; ResponseResult result = tsk_result.Result; string sResult = string.Format("optime={0}rnhttpstatucode={1}rnerrors={2}rnresult={3}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), result.CurrHttpStatuCode, result.ExceptionMsg, result.ResponseData); return sResult; }

 2.4)模拟get提交一个数据并包含普通参数(基于HttpClientAssisterAsync这个核心类).

 private async Task<string> GetGetResult_Async()         {             IWebProxy eProxy = this.getWebProxy();             HttpClientAssisterAsync htool = new HttpClientAssisterAsync();             Dictionary<string, string> dictHeader = new Dictionary<string, string>();             string sUserAgent = this.tbxUserAgent.Text.Trim();             if (!string.IsNullOrEmpty(this.tbx_Hedaer_Key.Text) && !string.IsNullOrEmpty(this.tbx_Hedaer_Value.Text))             {                 dictHeader[this.tbx_Hedaer_Key.Text.Trim()] = this.tbx_Hedaer_Value.Text.Trim();             }             Encoding eEncode = this.GetEncoding();             dictHeader["Accept-Language"] = "zh-CN,zh;q=0.8";             dictHeader["Cache-Control"] = "max-age=0";             string sAccess = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";             Task<ResponseResult> tsk_rsp = htool.SendRequest(new RequestParams() {                 URL=this.tbxUrl.Text,                 Method="GET", //在这里设置Get                 Timeout=30,                   PostData= this.tbxPostData.Text,                    WriteEnc=Encoding.Default,                     ReaderEnc=eEncode,                      WithCookie = this.btnWithCookie.Checked,                        DictRequestHeaderKeyValues=dictHeader,                         Accept=sAccess,                          UserAgent=sUserAgent,                           KeepAlive=false,                            EnableP3P=false,                            AllowRedrect=false,                             WebProxy=eProxy             });             await tsk_rsp;             ResponseResult result = tsk_rsp.Result;             return string.Format("optime={0}rnhttpstatucode={1}rnerror={2}rnresult={3}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), result.CurrHttpStatuCode, result.ExceptionMsg, result.ResponseData);         }

 

赞(0) 打赏
未经允许不得转载:张拓的天空 » 分享一个自己封装且一直在维护的依赖.net4.5的http异步组包工具类(支持get,post( 表单 ,json, 包含图片等文件的流提交) ,cookie管理,自动跳转,代理IP,https的支持,高并发的配置等等
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

专业的IT技术经验分享 更专业 更方便

联系我们本站主机

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏