namespace TrainInfomationProviderService.Web { using System; using System.Net; using FSLib.Network.Http; class HttpWebClient : HttpClient { public HttpWebClient() : base(null, new WebHandler()) { } } class WebHandler : HttpHandler { public WebHandler() { var proxy = System.Configuration.ConfigurationManager.AppSettings["web_proxy"]; if (!proxy.IsNullOrEmpty()) { _proxy = new WebProxy(new Uri(proxy)); var username = System.Configuration.ConfigurationManager.AppSettings["web_proxy_username"]; if (!proxy.IsNullOrEmpty()) { _proxy.UseDefaultCredentials = false; _proxy.Credentials = new NetworkCredential(username, System.Configuration.ConfigurationManager.AppSettings["web_proxy_password"]); } } } WebProxy _proxy; /// /// 获得用于发送请求的Request对象 /// /// /// /// public override HttpWebRequest GetRequest(Uri uri, HttpMethod method, HttpContext context) { var request = base.GetRequest(uri, method, context); if (_proxy != null) request.Proxy = _proxy; return request; } } }