/// <summary> /// 代理设置 /// </summary> /// <param name="request"></param> public static void SetProxySetting(WebRequest request, ProxySettingEntity Proxy) { if (Proxy == null) { return; } WebProxy webProxy = WebProxy.GetDefaultProxy(); if (Proxy.Ip != null && Proxy.Ip != "" && Proxy.Port != 0) { webProxy.Address = new Uri("http://" + Proxy.Ip + ":" + Proxy.Port + "/"); if (!string.IsNullOrEmpty(Proxy.UserName) && !string.IsNullOrEmpty(Proxy.Password)) { webProxy.Credentials = new NetworkCredential(Proxy.UserName, Proxy.Password); } } request.Proxy = webProxy; }
/// <summary> /// 测试代理配置 /// </summary> /// <param name="setting">代理信息</param> public static bool TestProxy(ProxySettingEntity setting, TestEntity te) { if (setting == null) { return(false); } HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(te.TestUrl); WebProxy proxy = WebProxy.GetDefaultProxy(); if (setting.Ip != null && setting.Ip != "" && setting.Port != 0) { proxy.Address = new Uri("http://" + setting.Ip + ":" + setting.Port + "/"); if (!string.IsNullOrEmpty(setting.UserName) && !string.IsNullOrEmpty(setting.Password)) { proxy.Credentials = new NetworkCredential(setting.UserName, setting.Password); } } webRequest.Proxy = proxy; webRequest.Method = "Get"; webRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; CIBA)"; WebResponse response = webRequest.GetResponse(); string html = GetHtmlString(response, Encoding.GetEncoding(te.TestWebEncoding)); response.Close(); if (html.Contains(te.TestWebTitle)) { return(true); } else { return(false); } }