Пример #1
0
        protected override WebRequest GetWebRequest(Uri address)
        {
            HttpWebRequest request;

            if (_proxy == null)
            {
                request = base.GetWebRequest(address) as HttpWebRequest;
            }
            else
            {
                switch (_proxy.Type)
                {
                case NetProxyType.Http:
                    request       = base.GetWebRequest(address) as HttpWebRequest;
                    request.Proxy = _proxy.HttpProxy;
                    break;

                case NetProxyType.Socks5:
                    _proxyRequestHandler = _proxy.SocksProxy.Connect(address.Host, address.Port);

                    if (address.Scheme == "https")
                    {
                        IWebProxy httpProxy = _proxyRequestHandler.CreateLocalHttpProxyConnectTunnel();

                        request       = base.GetWebRequest(address) as HttpWebRequest;
                        request.Proxy = httpProxy;
                    }
                    else
                    {
                        IPEndPoint localTunnelEP = _proxyRequestHandler.CreateLocalTunnel();
                        Uri        proxyUri      = new Uri("http://" + localTunnelEP.Address.ToString() + ":" + localTunnelEP.Port + address.PathAndQuery);

                        request      = base.GetWebRequest(proxyUri) as HttpWebRequest;
                        request.Host = address.Host;
                    }
                    break;

                default:
                    throw new NotSupportedException("Proxy type not supported.");
                }
            }

            if (_timeout > 0)
            {
                request.Timeout = _timeout;
            }

            request.CookieContainer = _cookie;

            if (_ifModifiedSince > (new DateTime()))
            {
                request.IfModifiedSince = _ifModifiedSince;
            }

            if (_userAgent != null)
            {
                request.UserAgent = _userAgent;
            }

            request.KeepAlive = _keepAlive;

            request.AllowAutoRedirect = false;

            foreach (KeyValuePair <string, string> header in _headers)
            {
                switch (header.Key.ToLower())
                {
                case "accept":
                    request.Accept = header.Value;
                    break;

                case "connection":
                    request.KeepAlive = (header.Value.ToLower() == "keep-alive");
                    break;

                case "content-type":
                    request.ContentType = header.Value;
                    break;

                case "user-agent":
                    request.UserAgent = header.Value;
                    break;

                default:
                    request.Headers.Add(header.Key, header.Value);
                    break;
                }
            }

            return(request);
        }
Пример #2
0
        protected override WebRequest GetWebRequest(Uri address)
        {
            HttpWebRequest request = null;

            if (_proxy == null)
            {
                switch (_networkType)
                {
                case WebClientExNetworkType.IPv4Only:
                    if (IPAddress.TryParse(address.Host, out IPAddress ipv4))
                    {
                        if (ipv4.AddressFamily != System.Net.Sockets.AddressFamily.InterNetwork)
                        {
                            throw new WebException("WebClientEx current network type does not allow url address family: " + ipv4.AddressFamily.ToString());
                        }

                        request = base.GetWebRequest(address) as HttpWebRequest;
                    }
                    else
                    {
                        try
                        {
                            DnsClient   dns         = new DnsClient();
                            IPAddress[] ipAddresses = dns.ResolveIP(address.Host);

                            if (ipAddresses.Length == 0)
                            {
                                throw new WebException("WebClientEx could not resolve IPv4 address for host: " + address.Host);
                            }

                            foreach (IPAddress ipAddress in ipAddresses)
                            {
                                if (ipAddress.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                                {
                                    Uri newAddress = new Uri(address.Scheme + "://" + ipAddress.ToString() + ":" + address.Port + address.PathAndQuery);
                                    request      = base.GetWebRequest(newAddress) as HttpWebRequest;
                                    request.Host = address.Host;
                                    break;
                                }
                            }
                        }
                        catch (DnsClientException ex)
                        {
                            throw new WebException("WebClientEx could not resolve IPv4 address for host: " + address.Host, ex);
                        }

                        if (request == null)
                        {
                            throw new WebException("WebClientEx could not resolve IPv4 address for host: " + address.Host);
                        }
                    }
                    break;

                case WebClientExNetworkType.IPv6Only:
                    if (IPAddress.TryParse(address.Host, out IPAddress ipv6))
                    {
                        if (ipv6.AddressFamily != System.Net.Sockets.AddressFamily.InterNetworkV6)
                        {
                            throw new WebException("WebClientEx current network type does not allow url address family: " + ipv6.AddressFamily.ToString());
                        }

                        request = base.GetWebRequest(address) as HttpWebRequest;
                    }
                    else
                    {
                        try
                        {
                            DnsClient   dns         = new DnsClient(true);
                            IPAddress[] ipAddresses = dns.ResolveIP(address.Host, true);

                            if (ipAddresses.Length == 0)
                            {
                                throw new WebException("WebClientEx could not resolve IPv6 address for host: " + address.Host);
                            }

                            foreach (IPAddress ipAddress in ipAddresses)
                            {
                                if (ipAddress.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6)
                                {
                                    Uri newAddress = new Uri(address.Scheme + "://[" + ipAddress.ToString() + "]:" + address.Port + address.PathAndQuery);
                                    request      = base.GetWebRequest(newAddress) as HttpWebRequest;
                                    request.Host = address.Host;
                                    break;
                                }
                            }
                        }
                        catch (DnsClientException ex)
                        {
                            throw new WebException("WebClientEx could not resolve IPv6 address for host: " + address.Host, ex);
                        }

                        if (request == null)
                        {
                            throw new WebException("WebClientEx could not resolve IPv6 address for host: " + address.Host);
                        }
                    }
                    break;

                default:
                    request = base.GetWebRequest(address) as HttpWebRequest;
                    break;
                }
            }
            else
            {
                switch (_proxy.Type)
                {
                case NetProxyType.Http:
                    request       = base.GetWebRequest(address) as HttpWebRequest;
                    request.Proxy = _proxy.HttpProxy;
                    break;

                case NetProxyType.Socks5:
                    _proxyRequestHandler = _proxy.SocksProxy.Connect(address.Host, address.Port);

                    if (address.Scheme == "https")
                    {
                        IWebProxy httpProxy = _proxyRequestHandler.CreateLocalHttpProxyConnectTunnel();

                        request       = base.GetWebRequest(address) as HttpWebRequest;
                        request.Proxy = httpProxy;
                    }
                    else
                    {
                        IPEndPoint localTunnelEP = _proxyRequestHandler.CreateLocalTunnel();
                        Uri        proxyUri      = new Uri("http://" + localTunnelEP.Address.ToString() + ":" + localTunnelEP.Port + address.PathAndQuery);

                        request      = base.GetWebRequest(proxyUri) as HttpWebRequest;
                        request.Host = address.Host;
                    }
                    break;

                default:
                    throw new NotSupportedException("Proxy type not supported.");
                }
            }

            if (_timeout > 0)
            {
                request.Timeout = _timeout;
            }

            request.CookieContainer = _cookie;

            if (_ifModifiedSince > (new DateTime()))
            {
                request.IfModifiedSince = _ifModifiedSince;
            }

            if (_userAgent != null)
            {
                request.UserAgent = _userAgent;
            }

            request.KeepAlive = _keepAlive;

            request.AllowAutoRedirect = false;

            foreach (KeyValuePair <string, string> header in _headers)
            {
                switch (header.Key.ToLower())
                {
                case "accept":
                    request.Accept = header.Value;
                    break;

                case "connection":
                    request.KeepAlive = (header.Value.ToLower() == "keep-alive");
                    break;

                case "content-type":
                    request.ContentType = header.Value;
                    break;

                case "user-agent":
                    request.UserAgent = header.Value;
                    break;

                default:
                    request.Headers.Add(header.Key, header.Value);
                    break;
                }
            }

            return(request);
        }