Exemplo n.º 1
0
        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);
        }
Exemplo n.º 2
0
        public static string DownLHttpPostWithCookiesB2b(string url, CookieCollection cookie,
                                                         FormUrlEncodedContent postContent = null, bool useProxy = false)
        {
            var tmp   = "";
            var count = 0;

            while (true)
            {
                try
                {
                    var task = Task.Run(() =>
                                        (HttpPostCookiesB2b.CreateInstance()).DownloadString(url, cookie, postContent,
                                                                                             useProxy: useProxy));
                    if (!task.Wait(TimeSpan.FromSeconds(60)))
                    {
                        throw new TimeoutException();
                    }
                    tmp = task.Result;
                    break;
                }
                catch (WebException ex)
                {
                    if (ex.Response is HttpWebResponse r)
                    {
                        Log.Logger("Response code: ", r.StatusCode);
                    }
                    if (ex.Response is HttpWebResponse errorResponse &&
                        errorResponse.StatusCode == HttpStatusCode.Forbidden)
                    {
                        Log.Logger("Error 403 or 434");
                        return(tmp);
                    }

                    if (count >= 2)
                    {
                        Log.Logger($"Не удалось скачать за {count} попыток", url);
                        break;
                    }

                    Log.Logger("Не удалось получить строку", ex.Message, url);
                    count++;
                    Thread.Sleep(5000);
                }
                catch (Exception e)
                {
                    if (count >= 2)
                    {
                        Log.Logger($"Не удалось скачать за {count} попыток", url);
                        break;
                    }

                    switch (e)
                    {
                    case AggregateException a
                        when a.InnerException != null && a.InnerException.Message.Contains("(404) Not Found"):
                        Log.Logger("404 Exception", a.InnerException.Message, url);
                        goto Finish;

                    case AggregateException a
                        when a.InnerException != null && a.InnerException.Message.Contains("(403) Forbidden"):
                        Log.Logger("403 Exception", a.InnerException.Message, url);
                        goto Finish;

                    case AggregateException a when a.InnerException != null &&
                        a.InnerException.Message.Contains(
                            "The remote server returned an error: (434)"):
                        Log.Logger("434 Exception", a.InnerException.Message, url);
                        goto Finish;
                    }

                    Log.Logger("Не удалось получить строку", e, url);
                    count++;
                    Thread.Sleep(5000);
                }
            }

Finish:
            return(tmp);
        }