分享一个自己封装且一直在维护的依赖.net4.5的http异步组包工具类(支持get,post( 表单 ,json, 包含图片等文件的流提交) ,cookie管理,自动跳转,代理IP,https的支持,高并发的配置等等
2024-09-01 22:35:42
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}\r\nhttpstatucode={1}\r\nerror={2}\r\nresult={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}\r\nhttpstatucode={1}\r\nerrors={2}\r\nresult={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}\r\nhttpstatucode={1}\r\nerror={2}\r\nresult={3}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), result.CurrHttpStatuCode, result.ExceptionMsg, result.ResponseData);
}
最新文章
- 关于GRUB2
- table中bordercolor属性设置后最新ie浏览器或firefox中不显示边线,借助table的css来实现边线
- KnockoutJS 3.X API 第二章 数据监控(1)视图模型与监控
- 转--Oracle 审计和测试操作
- PrettyProgressBar
- SpringMVC——hello SpringMVC
- Html笔记(十)XHTML XML
- clistctrl 虚拟列表
- JQuery中 json 和字符串直接相互转换
- 机器学习实战1-K均值
- Ajax上传图片以及上传之前先预览
- Django_简介
- RDD
- java里的基本数据类型和引用数据类型
- Mac流量监控/硬盘监控小工具
- JavaScript闭包函数&;箭头函数调用与执行
- CentOS7.5下安装Python3.7 --python3
- 【已解决】ERR_BLOCKED_BY_XSS_AUDITOR:Chrome 在此网页上检测到了异常代码:解决办法
- [UE4]手持多把枪的位置调节
- WPF datagrid 设置表头线与颜色、透明度