コード例 #1
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
            {
            }
        }