Share a self packaged and maintained HTTP asynchronous package building tool class that relies on. Net4.5 (support get, post (form, JSON, stream submission of files including images), cookie management, automatic jump, proxy IP, HTTPS support, high concurrency configuration, etc

Time:2020-10-27

1.) nuget installation:

Search ConfigLab.Comp Install the latest version

2.)

2.1) simulate post form submission and include common parameters and a picture file (based on the core class httpfileuploadassisterasync)

private async Task GetPostResultForFile_Async()
        {
            string sRspResult = "";
            IWebProxy eWebProxy =  this.getWebProxy (); // proxy IP settings
            Dictionary dictHeader = new Dictionary();
            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 dictimg = new dictionary(); // file information to be submitted with parameters, key = fullfilename, value = Dictionary of parameter name in the content of post
            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(); // enter the request address
            string sPostData =  this.tbxPostData.Text . trim(); // synchronous submitted text parameters: 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";
            //Asynchronous mode
            Httpfileuploadassisterasync httool = new httpfileuploadassisterasync(); // key classes
            Task tsk_rrs = htool.SendRequest(new RequestParamsWithFile()
            {
                URL = sUrl,
                Method = "POST",
                Timeout = 10,
                PostData = sPostData,
                Dictfilename2postname = dictimg, // multiple files can be passed at the same time
                WriteEnc = Encoding.UTF8,
                ReaderEnc = GetEncoding(),
                WithCookie =  this.btnWithCookie.Checked , // automatically maintain cookies
                Dictrequestheaderkeyvalues = dictheader, // custom header
                Accept = sAccess,
                UserAgent = sUserAgent,
                KeepAlive = true,
                EnableExpect100Continue = false,
                File_ ContentType = sFile_ Contenttype, // file type transferred 
                Webproxy = ewebproxy // proxy IP
            });
            await tsk_rrs;
            ResponseResult rrs_rspData = tsk_rrs.Result;
            sRspResult = rrs_rspData.ResponseData;
            return sRspResult;
        }

2.2) simulate post form submission and include common parameters (based on the core class of httpclient assistant async)

private async Task  GetPostResult_Async()
        {
            IWebProxy eProxy = this.getWebProxy();
            HttpClientAssisterAsync htool = new HttpClientAssisterAsync();
            Dictionary dictHeader = new Dictionary();
            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  tsk_result = htool.SendRequest(new RequestParams() {
                URL = this.tbxUrl.Text.Trim(),
                Method = "POST",
                Timeout = 30, // unit second
                PostData =  this.tbxPostData.Text , // form data, u = 1 & P = 2
                WriteEnc = Encoding.UTF8,
                ReaderEnc = GetEncoding(),
                WithCookie =  this.btnWithCookie.Checked , // automatically manage cookies
                Dictrequestheaderkeyvalues = dictheader, // custom header information
                Accept = "*/*",
                UserAgent = sUserAgent,
                KeepAlive = false,
                EnableP3P = false,
                Allowredrect = false, // allow auto jump
                 EnableExpect100Continue=true,
                Webproxy = eproxy // proxy 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) simulate post to submit a JSON data with common parameters (based on the core class httpclientasisterasync)

private async Task getResultForPostJson_Async()
        {
            IWebProxy eProxy = this.getWebProxy();
            HttpClientAssisterAsync htool = new HttpClientAssisterAsync();
            Dictionary dictHeader = new Dictionary();
            string sUserAgent = this.tbxUserAgent.Text.Trim();
            Dictheader ["content type"] = Application / JSON; charset = UTF-8 "; // key points!!!!
            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 tsk_result = htool.SendRequest(new RequestParams()
            {
                URL = this.tbxUrl.Text.Trim(),
                Method = "POST",
                Timeout = 30,
                PostData =  this.tbxPostData.Text , // JSON data
                WriteEnc = Encoding.UTF8,
                ReaderEnc = GetEncoding(),
                WithCookie =  this.btnWithCookie.Checked , // automatically manage cookies
                Dictrequestheaderkeyvalues = dictheader, // custom header 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) simulate get to submit a data with common parameters (based on the core class of httpclient assistant async)

private async Task GetGetResult_Async()
        {
            IWebProxy eProxy = this.getWebProxy();
            HttpClientAssisterAsync htool = new HttpClientAssisterAsync();
            Dictionary dictHeader = new Dictionary();
            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 tsk_rsp = htool.SendRequest(new RequestParams() { 
               URL=this.tbxUrl.Text,
                Method = "get", // set get here
                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);
        }