55 lines
1.3 KiB
C#
55 lines
1.3 KiB
C#
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">当前请求的HTTP方法</param>
|
|
/// <param name="context">当前的上下文 <see cref="HttpContext" /></param>
|
|
/// <returns></returns>
|
|
public override HttpWebRequest GetRequest(Uri uri, string method, HttpContext context)
|
|
{
|
|
var request = base.GetRequest(uri, method, context);
|
|
if (_proxy != null)
|
|
request.Proxy = _proxy;
|
|
|
|
return request;
|
|
}
|
|
}
|
|
}
|