【发布时间】:2015-02-28 09:02:30
【问题描述】:
我遇到了一个奇怪的情况。我有一个下载页面的方法,如下所示:
public static bool DownloadPageContent(string url,
out string content,
ref int statusCode,
CookieContainer cookie = null,
int maxAttempt = 1,
Encoding encoding = null)
{
int i = 0;
using (var client = new CookieAwareWebClient())
{
if (encoding != null)
client.Encoding = encoding;
while (i < maxAttempt)
{
try
{
i++;
if (cookie != null)
client.CookieContainer = cookie;
client.Headers.Add("user-agent", "Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko");
content = client.DownloadString(url);
statusCode = 200;
return true;
}
catch(WebException ex)
{
/* some error handling code*/
_logger.Log(LogLevel.Error, ex);
}
catch (Exception ex)
{
_logger.Log(LogLevel.Error, ex);
}
}
content = "";
return false;
}
这是StackTrace,它发生在content = client.DownloadString(url):
System.Net.WebException: 无法连接到远程服务器 ---> System.Net.Sockets.SocketException:试图访问一个 访问权限 xxx.xxx.xxx:80] 禁止的套接字 System.Net.Sockets.Socket.DoConnect(端点 endPointSnapshot, SocketAddress socketAddress) +208ı xxx.xxx.xxx:80 konum: System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress) konum: System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& 地址,ConnectSocketState 状态,IAsyncResult asyncResult, 例外&例外) --- İç özel durum yığını izlemesinin sonu --- konum:System.Net.WebClient.DownloadDataInternal(Uri 地址,WebRequest& 请求) konum: System.Net.WebClient.DownloadString(Uri 地址) konum: System.Net.WebClient.DownloadString(字符串地址) konum: Kokoshome_Product_Downloader.Helpers.SiteHelpers.DownloadPageContent(String url, String& 内容, Int32& statusCode, CookieContainer cookie, Int32 maxAttempt, Encoding 编码)
奇怪的是,如果我打开Fiddler2 并运行程序,它就可以正常运行!这段代码曾经可以工作,我使用Fiddler 已经有一段时间了,据我所知,我没有更改任何选项。但现在出了点问题,我想不通。那么可能是什么问题呢?
注意:我的互联网连接没有其他问题。我可以在没有Fiddler 的情况下连接互联网。而我的操作系统是Windows 8.1。
更新
当我尝试连接MySQL 数据库和FTP 服务器时,我遇到了同样的异常。这也不适用于Fiddler。我做了一些研究并在Windows Sockets Error Codes中找到了有关错误的信息
WSAEACCES 10013
权限被拒绝。试图以某种方式访问套接字 被其访问权限禁止。一个例子是使用广播 未使用设置广播权限的 sendto 地址 setsockopt(SO_BROADCAST)。 WSAEACCES 的另一个可能原因 错误是当调用绑定函数时(在 Windows NT 4.0 上 SP4 及更高版本),另一个应用程序、服务或内核模式驱动程序是 绑定到具有独占访问权限的同一地址。这种独家访问 是带有 SP4 及更高版本的 Windows NT 4.0 的一项新功能,并且是 通过使用 SO_EXCLUSIVEADDRUSE 选项实现。
但我仍然不知道如何解决它。
【问题讨论】:
标签: c# fiddler socketexception