当服务作为LocalService帐户运行时,WebClient无法正常工作

时间:2022-09-01 15:34:31

I have a Windows Service that sends a Post Request to a url:

我有一个Windows服务,它发送一个发布请求到一个网址:

            System.Net.ServicePointManager.Expect100Continue = false;
            using (WebClient wc = new WebClient())
            {
                wc.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
                string HtmlResult = wc.UploadString(url, myParameters);

                Console.WriteLine(HtmlResult);
            }

This works only if the Service is run As Administrator and when run in a console application. When run as LocalService Account i get a Timeout.

仅当服务以管理员身份运行并在控制台应用程序中运行时,此方法才有效。当作为LocalService帐户运行时,我得到一个超时。

What can i do about this?

我该怎么办?

2 个解决方案

#1


I would guess your problem is that a localservice is not allowed to access remote resources. Thus you need to use an account that is allowed to use remote resources like network service or a user account created with specific rights that fit your needs (you might also need to access a local folder to read/write data) which is the best practice imho.

我猜你的问题是不允许本地服务访问远程资源。因此,您需要使用允许使用远程资源的帐户,例如网络服务或使用适合您需要的特定权限创建的用户帐户(您可能还需要访问本地文件夹以读取/写入数据)这是最佳做法恕我直言。

#2


Once running a network-related code under LocalService account, you're appearing as anonymous user. So it's quite likely that guest access is disabled on to your premises. So it's may be worth to replace LocalService with NetworkService (which is specially designed for network staff). http://windowsitpro.com/systems-management/understanding-local-service-and-network-service-accounts

在LocalService帐户下运行与网络相关的代码后,您将显示为匿名用户。因此很可能禁止访客访问您的场所。因此,使用NetworkService(专为网络人员设计)替换LocalService可能是值得的。 http://windowsitpro.com/systems-management/understanding-local-service-and-network-service-accounts

#1


I would guess your problem is that a localservice is not allowed to access remote resources. Thus you need to use an account that is allowed to use remote resources like network service or a user account created with specific rights that fit your needs (you might also need to access a local folder to read/write data) which is the best practice imho.

我猜你的问题是不允许本地服务访问远程资源。因此,您需要使用允许使用远程资源的帐户,例如网络服务或使用适合您需要的特定权限创建的用户帐户(您可能还需要访问本地文件夹以读取/写入数据)这是最佳做法恕我直言。

#2


Once running a network-related code under LocalService account, you're appearing as anonymous user. So it's quite likely that guest access is disabled on to your premises. So it's may be worth to replace LocalService with NetworkService (which is specially designed for network staff). http://windowsitpro.com/systems-management/understanding-local-service-and-network-service-accounts

在LocalService帐户下运行与网络相关的代码后,您将显示为匿名用户。因此很可能禁止访客访问您的场所。因此,使用NetworkService(专为网络人员设计)替换LocalService可能是值得的。 http://windowsitpro.com/systems-management/understanding-local-service-and-network-service-accounts