Beispiel #1
0
        public bool IsAvailable(UseSpecifiedUriWebProxy proxy)
        {
            var timeout       = TimeSpan.FromSeconds(1d);
            var validator     = new ProxyValidator(proxy);
            var targetAddress = new Uri(this._targetUrl);

            return(validator.Validate(targetAddress, timeout) == HttpStatusCode.OK);
        }
Beispiel #2
0
        /// <summary>
        /// 把代理返回给代理池
        /// </summary>
        /// <param name="proxy">代理</param>
        /// <param name="statusCode">通过此代理请求数据后的返回状态</param>
        public void ReturnProxy(UseSpecifiedUriWebProxy proxy, HttpStatusCode statusCode)
        {
            if (proxy == null)
            {
                return;
            }
            var key = $"{proxy.Uri.Host}:{proxy.Uri.Port}";

            if (!_allProxy.ContainsKey(key))
            {
                return;
            }
            Proxy p = _allProxy[key];

            switch (statusCode)
            {
            case HttpStatusCode.OK:
                p.SetFailedNum(0);
                p.SetReuseTime(_reuseInterval);
                p.RecordResponse();
                break;

            case HttpStatusCode.Forbidden:
                p.Fail();
                p.SetReuseTime(_reuseInterval * p.FailedNum);
                break;

            case HttpStatusCode.NotFound:
                p.Fail();
                p.SetReuseTime(_reuseInterval * p.FailedNum);
                break;

            default:
                p.Fail();
                p.SetReuseTime(_reuseInterval * p.FailedNum);
                break;
            }
            if (p.FailedNum > 20)
            {
                return;
            }
            if (ProxyValidator != null && p.FailedNum % 3 == 0 && ProxyValidator.IsAvailable(proxy))
            {
                return;
            }
            lock (_locker)
            {
                _proxyQueue.Add(p);
            }
        }
Beispiel #3
0
        public void ReturnProxy(UseSpecifiedUriWebProxy host, HttpStatusCode statusCode)
        {
            if (host == null)
            {
                return;
            }
            var key = $"{host.Uri.Host}:{host.Uri.Port}";

            if (!_allProxy.ContainsKey(key))
            {
                return;
            }
            Proxy p = _allProxy[key];

            switch (statusCode)
            {
            case HttpStatusCode.OK:
                p.SetFailedNum(0);
                p.SetReuseTime(_reuseInterval);
                p.RecordResponse();
                break;

            case HttpStatusCode.Forbidden:
                p.Fail();
                p.SetReuseTime(_reuseInterval * p.FailedNum);
                break;

            case HttpStatusCode.NotFound:
                p.Fail();
                p.SetReuseTime(_reuseInterval * p.FailedNum);
                break;

            default:
                p.Fail();
                p.SetReuseTime(_reuseInterval * p.FailedNum);
                break;
            }
            if (p.FailedNum > 20)
            {
                return;
            }
            if (p.FailedNum % 3 == 0 && !ProxyUtil.ValidateProxy(p.HttpHost.Uri.Host, p.HttpHost.Uri.Port))
            {
                return;
            }
            lock (this)
            {
                _proxyQueue.Add(p);
            }
        }
Beispiel #4
0
        public bool IsAvailable(UseSpecifiedUriWebProxy proxy)
        {
            var userName = default(string);
            var password = default(string);

            if (proxy.Credentials != null)
            {
                var credentials = proxy.Credentials.GetCredential(null, null);
                userName = credentials?.UserName;
                password = credentials?.Password;
            }

            var targetUri = new Uri(_targetUrl);
            var status    = ProxyValidate.Validate(proxy.Uri.Host, proxy.Uri.Port, userName, password, targetUri);

            return(status == HttpStatusCode.OK);
        }
Beispiel #5
0
        public Proxy(UseSpecifiedUriWebProxy httpHost, int reuseTimeInterval = 1500)
        {
            HttpHost = httpHost;

            CanReuseTime = DateTimeUtils.GetCurrentTimeStamp() + reuseTimeInterval * 100;
        }
Beispiel #6
0
 public SingleProxyPool(UseSpecifiedUriWebProxy proxy)
 {
     _proxy = proxy;
 }
Beispiel #7
0
 public void ReturnProxy(UseSpecifiedUriWebProxy host, HttpStatusCode statusCode)
 {
 }
 /// <summary>
 /// 构造方法
 /// </summary>
 /// <param name="url">HTTP代理的链接</param>
 public SingleProxyPool(string url)
 {
     _proxy = new UseSpecifiedUriWebProxy(new System.Uri(url));
 }
Beispiel #9
0
        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="proxy">实际代理信息</param>
        /// <param name="reuseTimeInterval">代理不被再次使用的间隔</param>
        public Proxy(UseSpecifiedUriWebProxy proxy, int reuseTimeInterval = 1500)
        {
            WebProxy = proxy;

            CanReuseTime = DateTimeUtil.GetCurrentUnixTimeNumber() + reuseTimeInterval * 100;
        }