Light12306/TrainInfomationProviderService/Web/WebClient.cs

54 lines
1.2 KiB
C#
Raw Normal View History

2015-07-16 21:00:51 +08:00
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;
/// <summary>
/// 获得用于发送请求的Request对象
/// </summary>
/// <param name="uri"></param>
/// <param name="method"></param>
/// <returns></returns>
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;
}
}
}