public CookieCollection CookieValue() { var cookieContainer = new CookieContainer(); var handler = new HttpClientHandler { CookieContainer = cookieContainer, AllowAutoRedirect = true }; if (AppBuilder.UserProxy) { var prixyEntity = ProxyLoader.getRandomProxy(); var proxy = new WebProxy { Address = new Uri($"http://{prixyEntity.Ip}:{prixyEntity.Port}"), BypassProxyOnLocal = false, UseDefaultCredentials = false, Credentials = new NetworkCredential( userName: prixyEntity.User, password: prixyEntity.Pass) }; handler.Proxy = proxy; } var client = new HttpClient(handler); HttpPostCookiesB2b.FillUserAgent(client); var response = client.GetAsync(BaseUrl); var res = response.Result; _ = res.Content.ReadAsStringAsync().Result; var cookies = handler.CookieContainer.GetCookies(new Uri(BaseUrl)); return(cookies); }
public string DownloadString(string url, CookieCollection cookie = null, FormUrlEncodedContent postContent = null, bool useProxy = false) { var cookieContainer = new CookieContainer(); if (cookie != null) { cookieContainer.Add(new Uri("https://www.b2b-center.ru/"), cookie); } var httpClientHandler = new HttpClientHandler { AllowAutoRedirect = true, CookieContainer = cookieContainer, UseCookies = true }; if (useProxy) { var prixyEntity = ProxyLoader.getRandomProxy(); var proxy = new WebProxy { Address = new Uri($"http://{prixyEntity.Ip}:{prixyEntity.Port}"), BypassProxyOnLocal = false, UseDefaultCredentials = false, Credentials = new NetworkCredential( userName: prixyEntity.User, password: prixyEntity.Pass) }; httpClientHandler.Proxy = proxy; } using (var client = new HttpClient(httpClientHandler)) { //client.DefaultRequestHeaders.Clear(); FillUserAgent(client); var response = client.GetAsync(url); var res = response.Result; if (res.StatusCode == HttpStatusCode.NotFound) { Console.WriteLine(res.StatusCode); } return(res.Content.ReadAsStringAsync().Result); } }