控制WebRequest代理

private void ProxySetting(WebRequest request)

    WebProxy proxy = WebProxy.GetDefaultProxy();//获取IE缺省设置 //如果缺省设置为空,则有可能是根本不需要代理服务器,如果此时配置文件中也未配置则认为不需Proxy  
     if (proxy.Address == null)//按配置文件创建Proxy 地置  
     { 
         string address = "http://"+ConfigurationUtil.ProxyAddress + ":" + ConfigurationUtil.ProxyPort; 
         proxy.Address = new Uri(address); 
     } 
            
     if (proxy.Address != null)//如果地址为空,则不需要代理服务器 
     { 
         proxy.Credentials = new NetworkCredential(ConfigurationUtil.UserName, ConfigurationUtil.Password);//从配置封装参数中创建 
         request.Proxy = proxy;//赋予 request.Proxy  
     }
}

调用:

if (!string.IsNullOrEmpty(ConfigurationUtil.IsProxy)
               && Convert.ToBoolean(ConfigurationUtil.IsProxy))

{
     ProxySetting(request);
}

获取配置:

internal class ConfigurationUtil
    {
        public static string IsProxy
        {
            get
            {
                if (string.IsNullOrEmpty(_IsProxy))
                {
                    _IsProxy = ConfigurationManager.AppSettings["IsProxy"];
                }
                return _IsProxy;
            }
        }

        public static string ProxyAddress
        {
            get
            {
                if (string.IsNullOrEmpty(_proxyAddress))
                {
                    _proxyAddress = ConfigurationManager.AppSettings["ProxyAddress"];
                }
                return _proxyAddress;
            }
        }

        public static string ProxyPort
        {
            get
            {
                if (string.IsNullOrEmpty(_proxyPort))
                {
                    _proxyPort = ConfigurationManager.AppSettings["ProxyPort"];
                }
                return _proxyPort;
            }
        }

        public static string UserName
        {
            get
            {
                if (string.IsNullOrEmpty(_userName))
                {
                    _userName = ConfigurationManager.AppSettings["UserName"];
                }
                return _userName;
            }
        }

        public static string Password
        {
            get
            {
                if (string.IsNullOrEmpty(_password))
                {
                    _password = ConfigurationManager.AppSettings["Password"];
                }
                return _password;
            }
        }

        private static string _proxyAddress;
        private static string _proxyPort;
        private static string _userName;
        private static string _password;
        private static string _IsProxy;
    }

配置文件:

<?xml version="1.0" encoding="utf-8"?>
<appSettings>
  <add key="IsProxy" value="False"/>
  <add key="ProxyAddress" value="192.168.1.222"/>
  <add key="ProxyPort" value="8080"/>
  <add key="UserName" value="xxxx"/>
  <add key="Password" value="123"/>
</appSettings>

Post:

protected string DoHttpPostAction(string url, string xml)
        {
            string result = null;

            Encoding encoding = Encoding.UTF8;
            byte[] postData = encoding.GetBytes(xml);

            HttpWebRequest request =
                (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(url);

            _log.DebugFormat("请求Ucp Url:{0} xml:{1}{2}", url, Environment.NewLine, xml);

            request.Method = "POST";
            request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; POTU(RR:27062616:0:5076910); .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.590; .NET CLR 3.5.20706; MAXTHON 2.0)";
            request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = postData.Length;

            if (!string.IsNullOrEmpty(ConfigurationUtil.IsProxy)
                && Convert.ToBoolean(ConfigurationUtil.IsProxy))
            {
                ProxySetting(request);
            }
            else
            {
                request.Proxy = null;
            }
            request.Timeout = 10000;
            try
            {
                Stream newStream = request.GetRequestStream();
                newStream.Write(postData, 0, postData.Length);
                newStream.Close();

                HttpWebResponse response;
                response = (HttpWebResponse)request.GetResponse();

                if (response.StatusCode == HttpStatusCode.OK)
                {
                    StreamReader reader = new StreamReader(response.GetResponseStream(),
                        Encoding.UTF8);
                    result = reader.ReadToEnd();
                    reader.Close();
                    response.Close();
                }
            }
            catch (WebException ex)
            {


            }
            finally
            {
                request.Abort();
            }

            return result;
        }

posted @ 2011-04-26 19:19  Ryan R  阅读(397)  评论(0编辑  收藏  举报