简体   繁体   English

WebClient作为本地系统运行时无法下载文件

[英]WebClient unable to download file when run as Local System

I have a windows service running as the default Local System (on Windows Server 2008 R2) that attempts to pull down a PDF from a user-provided URL. 我有一个Windows服务作为默认的本地系统运行(在Windows Server 2008 R2上),试图从用户提供的URL下拉PDF。 If that URL is in the local intranet there's a happy ending. 如果该URL在本地Intranet中,那么结局很不错。 If the URL points to a more remote URL (say from irs.gov), I get an exception: 如果该URL指向更远程的URL(例如,来自irs.gov的URL),则会出现异常:

System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond ... System.Net.WebException:无法连接到远程服务器---> System.Net.Sockets.SocketException:连接尝试失败,因为一段时间后被连接方未正确响应,或者由于连接的主机而建立的连接失败未能回应...

If I change the the "Logon as" account from Local System to my account (for instance), the request goes through. 如果我将“登录为”帐户从本地系统更改为我的帐户(例如),请求将通过。 But I'm reluctant to run a windows service as a real user (am I being too conservative?). 但是我不愿意以实际用户身份运行Windows服务(我太保守了吗?)。

The code I'm using seems pretty simple: 我正在使用的代码看起来很简单:

string destinationFileName = ...;
Uri uri = ...;
using (var wc = new WebClient()) {
    wc.DownloadFile(uri.AbsoluteUri, destinationFileName );
}

Do I need to get some kind of IE settings and add them to the WebClient instance? 我是否需要获取某种IE设置并将其添加到WebClient实例? If so, what? 如果是这样,该怎么办?

Thanks for any hints or pointers! 感谢您的任何提示或指示!

Try running your service with NT AUTHORITY\\NETWORK SERVICE. 尝试使用NT AUTHORITY \\ NETWORK SERVICE运行服务。 If that doesn't do the trick, Create a user account with appropriately restricted rights and run your service with that account. 如果这样不能解决问题,请创建一个具有适当限制权限的用户帐户,然后使用该帐户运行服务。

Also, if you're running on Windows Server 2008 R2 give the Managed Service Accounts topic on Technet a look. 另外,如果您在Windows Server 2008 R2上运行,请在Technet上查看“ 托管服务帐户”主题。

LocalSystem doesn't have access to network resources. LocalSystem无权访问网络资源。 Use NETWORK SERVICE account. 使用 NETWORK SERVICE帐户。

EDIT: LocalSystem most likely does not have write permissions to the filesystem path you are using. 编辑:LocalSystem很可能没有对您使用的文件系统路径的写权限。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM