控制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;
        }

转载于:https://www.cnblogs.com/ruanzb/archive/2011/04/26/2029663.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值