2014-03-13 98 views
1

我试图沿着下面的代码的东西运行在Windows Server 2012 R2机器上的C#的Web客户端应用程序:权限问题与.NET的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的问题,我会是什么任何暗示感激我可能做错了!

回答

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> 
在应用这些可以通过编程通过加载代理信息到客户对象设定