您的位置:首页 > 其它

WebClient in Windows Service

2012-06-01 09:28 183 查看
前提:

1.如果你访问网络需要使用代理

2.如果你控制台程序使用WebClient访问或下载文件时正常,但作为Windows Service使用 WebClient 时失败。其中报错包括但不限于:操作超时(The operation has timed out) / 无法连接到远程服务器(Unable to connect to the remote server)

请尝试在代码中定义代理地址:

var client = new WebClient();
if (!string.IsNullOrEmpty(System.Configuration.ConfigurationManager.AppSettings["WebProxyUri"]))
{
IWebProxy proxyObject = new WebProxy(System.Configuration.ConfigurationManager.AppSettings["WebProxyUri"], true, new[] { @"172.*" });
client.Proxy = proxyObject;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: