WebClient in Windows Service

来源:互联网 发布:tensorflow reuse 编辑:程序博客网 时间:2024/05/29 02:05

前提:

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;            }