.NET Web客户端应用程序的权限问题

时间:2014-03-13 04:15:34

标签: c# .net permissions

我正尝试在Windows Server 2012 R2计算机上运行C#Web客户端应用程序,其中包含以下代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;

namespace willitwork
{
    class Program
    {
        static void Main()
        {
                // Create web client.
                WebClient client = new WebClient();

                // Download string.
                string value = client.DownloadString("http://www.dotnetperls.com/");

                // Write values.
                Console.WriteLine("--- WebClient result ---");
                Console.WriteLine(value.Length);
                Console.WriteLine(value);
        }
    }
}

当我以管理员身份运行应用程序时,一切正常:

C:\moop>whoami
p10uc2\administrator

C:\moop>willitwork.exe
--- WebClient result ---
3336
<!doctype html><html><head><link rel=canonical href=http://www.dotnetperls.com><style>body{font:18px/1.45 verdana;position:relative;padding:0 0 330px;min-width:
750px;max-width:960px;margin:0 auto}...

    etc.

但是,部署后,应用程序将由系统启动。我切换到系统用户并再次运行应用程序,只是为了面对这个:

c:\moop>whoami
nt authority\system

c:\moop>willitwork.exe

Unhandled Exception: System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: No connection could be made becaus
e the target machine actively refused it 54.240.176.85:80
   at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress)
   at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state,
IAsyncResult asyncResult, Exception& exception)
   --- End of inner exception stack trace ---
   at System.Net.WebClient.DownloadDataInternal(Uri address, WebRequest& request)
   at System.Net.WebClient.DownloadString(Uri address)
   at System.Net.WebClient.DownloadString(String address)
   at willitwork.Program.Main()

由于某种原因,这似乎不是Windows 7的问题。我会很感激任何关于我可能做错的提示!

1 个答案:

答案 0 :(得分:2)

查看this page.

后找到解决方案

问题与代理有关,显然,应用程序使用IE的代理设置。在应用程序中,可以通过将代理信息加载到客户端对象中以编程方式设置它们:

client.Proxy = new WebProxy(new Uri("http://proxy.oursite.com:8080"), true);

或通过添加以下内容来更改应用程序的app.conf文件:

<system.net>
  <defaultProxy>
    <proxy 
        usesystemdefault="True"
        proxyaddress=http://proxy.oursite.com:8080
        bypassonlocal="True"
    />
 </defaultProxy>
</system.net>