コード例 #1
0
        public static string Proxy_GetRequest2(string url)
        {
            try
            {
                WebClientEx webClient = new WebClientEx();

                //webClient.Encoding = Encoding.GetEncoding("gb2312");
                if (true)
                {
                    //IP: 帐号: tets1106密码: tets1106开通成功!http端口:808
                    //创建 代理服务器设置对象 的实例
                    System.Net.WebProxy wp = new System.Net.WebProxy("123.249.2.2:888");
                    //代理服务器需要验证
                    wp.BypassProxyOnLocal = false;
                    //用户名密码
                    wp.Credentials = new NetworkCredential("te1107", "te1107");
                    ////将代理服务器设置对象赋予全局设定
                    //System.Net.GlobalProxySelection.Select = wp;



                    webClient.Proxy = wp;
                }
                webClient.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36");
                Stream stream = webClient.OpenRead(url);

                StreamReader sr     = new StreamReader(stream);
                var          result = sr.ReadToEnd();

                Debug.WriteLine(url);
                return(result);
            }
            catch (IOException ioException)
            {
                throw;
            }
            catch (WebException ex)
            {
                if (ex.Response != null)
                {
                    switch (((System.Net.HttpWebResponse)(ex.Response)).StatusCode)
                    {
                    case HttpStatusCode.Unauthorized:
                        throw new UnauthorizedAccessException("UnauthorizedAccess", ex);

                    default:
                        throw;
                    }
                }
                else
                {
                    throw;
                }
            }
            finally
            {
            }
        }
コード例 #2
0
        public static string Proxy_GetRequest(string url, string ip_port)
        {
            try
            {
                WebClientEx webClient = new WebClientEx();

                webClient.Encoding = Encoding.GetEncoding("gb2312");
                if (true)
                {
                    WebProxy proxy = new WebProxy();
                    proxy.UseDefaultCredentials = false;
                    proxy.Address   = new Uri($"http://{ip_port}");
                    webClient.Proxy = proxy;
                }

                Stream stream = webClient.OpenRead(url);

                StreamReader sr     = new StreamReader(stream);
                var          result = sr.ReadToEnd();

                Debug.WriteLine(url);
                return(result);
            }
            catch (IOException ioException)
            {
                throw;
            }
            catch (WebException ex)
            {
                if (ex.Response != null)
                {
                    switch (((System.Net.HttpWebResponse)(ex.Response)).StatusCode)
                    {
                    case HttpStatusCode.Unauthorized:
                        throw new UnauthorizedAccessException("UnauthorizedAccess", ex);

                    default:
                        throw;
                    }
                }
                else
                {
                    throw;
                }
            }
            finally
            {
            }
        }
コード例 #3
0
        public static string Proxy_GetRequestAbyyun(string url)
        {
            try
            {
                WebClientEx webClient = new WebClientEx();

                webClient.Encoding = Encoding.GetEncoding("gb2312");
                if (true)
                {
                    //WebProxy proxy = new WebProxy();
                    //proxy.UseDefaultCredentials = true;
                    //proxy.Address = new Uri($"http://{ip_port}");
                    //webClient.Proxy = proxy;
                    //proxy.Credentials=new bas
                    ////////webClient.
                    ////////HttpTransportProperties.Authenticator basicauth = new HttpTransportProperties.Authenticator();
                    ////////basicauth.setUsername("username");  //服务器访问用户名 
                    ////////basicauth.setPassword("password"); //服务器访问密码
                    ////////options.setProperty(HTTPConstants.AUTHENTICATE, basicauth);
                    ////////---------------------
                    ////////作者:Jimstin
                    ////////来源:CSDN
                    ////////原文:https://blog.csdn.net/zhangjm_123/article/details/26581971
                    // 代理服务器
                    String proxyServer = "http-dyn.abuyun.com";
                    int    proxyPort   = 9020;

                    // 代理隧道验证信息
                    String proxyUser = "******";
                    String proxyPass = "******";

                    //Authenticator.setDefault(new ProxyAuthenticator(proxyUser, proxyPass));
                    //webClient.Credentials = GetCredentialCache("http-dyn.abuyun.com", proxyUser, proxyPass);

                    webClient.Headers.Add("Proxy-Authorization", GetAuthorization(proxyUser, proxyPass));
                }

                Stream stream = webClient.OpenRead(url);

                StreamReader sr     = new StreamReader(stream);
                var          result = sr.ReadToEnd();

                Debug.WriteLine(url);
                return(result);
            }
            catch (IOException ioException)
            {
                throw;
            }
            catch (WebException ex)
            {
                if (ex.Response != null)
                {
                    switch (((System.Net.HttpWebResponse)(ex.Response)).StatusCode)
                    {
                    case HttpStatusCode.Unauthorized:
                        throw new UnauthorizedAccessException("UnauthorizedAccess", ex);

                    default:
                        throw;
                    }
                }
                else
                {
                    throw;
                }
            }
            finally
            {
            }
        }