从共享驱动器运行的C#网络连接

时间:2012-09-10 14:17:05

标签: c# network-share

我正在尝试在C#代码中读取Web资源。不幸的是,代码仅在从本地驱动器运行时有效,并且在从网络共享运行时抛出异常(这就是我需要的)。

The underlying connection was closed: Unable to connect to the remote server.
   in System.Net.HttpWebRequest.GetResponse()
   .........
Caused by: 
An invalid argument was supplied
   in System.Net.Sockets.Socket..ctor(AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType)
   in System.Net.ServicePoint.GetConnection(PooledStream PooledStream, Object owner, Boolean async, IPAddress& address, Socket& abortSocket, Socket& abortSocket6, Int32 timeout)
   in System.Net.PooledStream.Activate(Object owningObject, Boolean async, Int32 timeout, GeneralAsyncDelegate asyncCallback)
   in System.Net.Connection.CompleteStartConnection(Boolean async, HttpWebRequest httpWebRequest)

我尝试过类似的代码(这会引发异常):

request = (HttpWebRequest)WebRequest.Create(url);

以及根据http://msdn.microsoft.com/en-us/library/2b86d684的直接套接字连接。

我怀疑Windows中存在某种防止网络连接的安全设置。有可能解决这个问题吗?通过AD政策?或者我的代码中有什么东西?

1 个答案:

答案 0 :(得分:3)

你可以从The Project Location is Not Trusted Dialog Box开始。引用:

  

默认情况下,UNC路径不是项目的受信任位置。您可以修改文件共享的安全策略以防止出现此对话框。有关详细信息,请参阅Configuring Security Policy