@Shayden

Преобразование HttpWebRequest в HttpClient?

Как здесь можно преобразовать привычный но устаревший httpwebrequest в httpclient асинхронного типа?
Часть одна сделана была(класс Connecter реализован в httpclient асинхронно, и то не без помощи:). Всю голову уже изломал:
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;
        }
  • Вопрос задан
  • 111 просмотров
Пригласить эксперта
Ответы на вопрос 1
Вот документация: https://docs.microsoft.com/ru-ru/dotnet/api/system...

В целом всё то же самое - заголовки, методы, тело.
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы