S
S
Shayden2021-09-28 04:32:43
C++ / C#
Shayden, 2021-09-28 04:32:43

Convert HttpWebRequest to HttpClient?

How can one convert the familiar but deprecated httpwebrequest into an asynchronous type httpclient here?
Part one was done (the Connecter class is implemented asynchronously in httpclient, and not without help :). I already broke my whole head:

public bool Auth()
        {
            Connecter connecter = new Connecter();
            HttpWebRequest newRequest = connecter.GetNewRequest(connecter.OriginUrl + "login", connecter.cookies);
            System.Net.ServicePointManager.Expect100Continue = false;
            System.Net.ServicePointManager.UseNagleAlgorithm = false;
            newRequest.ServicePoint.ConnectionLimit = 15;
            ServicePointManager.FindServicePoint(new Uri(connecter.OriginUrl));
            newRequest.KeepAlive = true;
            newRequest.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
            Dictionary<string, string> parameters = new Dictionary<string, string> {
                {
                    "email",
                    this.accountEmail
                },
                {
                    "password",
                    this.accountPass
                },
                {
                    "request_type",
                    "ajax"
                }
            };
            string input = "";
            HttpWebResponse response = connecter.MakeRequest(newRequest, connecter.cookies, parameters, 1);
            using (StreamReader reader = new StreamReader(response.GetResponseStream()))
            {
                if (!reader.EndOfStream)
                {
                    input = input + reader.ReadToEnd();
                }
            }
            response.Close();
            this.cookiesG = connecter.cookies;
            return (JsonConvert.DeserializeObject<Dictionary<string, object>>(input)["status"].ToString() == "success");
        }

 public bool getCaptcha()
        {
            if (this.gonaStop)
            {
                this.locked = true;
                this.stopped = false;
                this.blocked = true;
                this.requestTime = 0L;
            }
            this.locked = true;
            Connecter connecter = new Connecter
            {
                cookies = this.cookiesG
            };
            HttpWebRequest newRequest = connecter.GetNewRequest(connecter.OriginUrl + "/captcha/" + this.accountToken, connecter.cookies);
            System.Net.ServicePointManager.Expect100Continue = false;
            System.Net.ServicePointManager.UseNagleAlgorithm = false;
            if (TypeRequest == 1)
            {
                newRequest.Headers["X-Requested-With"] = "XMLHttpRequest";
            }
            if (TypeRequest == 2)
            {
                newRequest.Headers["Upgrade-Insecure-Requests"] = "1";
            }
            newRequest.ServicePoint.ConnectionLimit = 100;
            ServicePointManager.FindServicePoint(new Uri(connecter.OriginUrl));
            newRequest.KeepAlive = true;
            newRequest.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
            Dictionary<string, string> parameters = new Dictionary<string, string>();
            string input = "";
            parameters = null;
            HttpWebResponse response = connecter.MakeRequest(newRequest, connecter.cookies, parameters, 2);
            using (StreamReader reader = new StreamReader(response.GetResponseStream()))
            {
                if (!reader.EndOfStream)
                {
                    input = input + reader.ReadToEnd();
                }
            }
            response.Close();           
            this.RES = input;
            this.captcha = JsonConvert.DeserializeObject<Dictionary<string, object>>(input);
            if ((this.captcha != null) && (this.captcha["status"].ToString() == "success"))
            {
                return true;
            }
            if (((this.captcha != null) && (this.captcha["error"] != null)) && (this.captcha["error"].ToString() != "null"))
            {
                this.blocked = true;
            }
            return false;
        }

 public string getProfileInfo()
        {
            Connecter connecter = new Connecter
            {
                cookies = this.cookiesG
            };
            this.gonaStop = false;
            HttpWebRequest newRequest = connecter.GetNewRequest(connecter.OriginUrl + "profile", connecter.cookies);
            System.Net.ServicePointManager.Expect100Continue = false;
            System.Net.ServicePointManager.UseNagleAlgorithm = false;
            newRequest.ServicePoint.ConnectionLimit = 100;
            ServicePointManager.FindServicePoint(new Uri(connecter.OriginUrl));
            newRequest.KeepAlive = true;
            newRequest.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
            Dictionary<string, string> parameters = new Dictionary<string, string>();
            string input = "";
            parameters = null;
            HttpWebResponse response = connecter.MakeRequest(newRequest, connecter.cookies, parameters, 2);
            using (StreamReader reader = new StreamReader(response.GetResponseStream()))
            {
                if (!reader.EndOfStream)
                {
                    input = input + reader.ReadToEnd();
                }
            }
            this.RES = input;
            response.Close();
            Match match = new Regex("id=\"payment_details\">(.*?)<", RegexOptions.IgnoreCase).Match(input);
            if (match.Success)
            {
                input = match.Groups[1].Value;
            }
            this.PaymentDietals = input;
            Match match2 = new Regex("id=\"payment_type\">(.*?)<", RegexOptions.IgnoreCase).Match(this.RES);
            if (match2.Success)
            {
                this.PaymentType = match2.Groups[1].Value;
            }
            string str2 = "";
            Match match3 = new Regex("name=\"csrf_profile_token\" value=\"(.*?)\"", RegexOptions.IgnoreCase).Match(this.RES);
            if (match3.Success)
            {
                str2 = match3.Groups[1].Value;
            }
            return str2;
        }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vasily Bannikov, 2021-09-28
@vabka

Here is the documentation: https://docs.microsoft.com/ru-ru/dotnet/api/system...
In general, everything is the same - headers, methods, body.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question