【问题标题】:Why I get SocketException when downloading a page if I don't open Fiddler?如果我不打开 Fiddler,为什么在下载页面时会出现 SocketException?
【发布时间】: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


    【解决方案1】:

    几乎在所有情况下,这意味着您的防火墙正在阻止您的程序访问网络。当您使用 Fiddler 时,请求会在到达 Fiddler 的途中绕过防火墙,然后绕过防火墙,因为您拥有允许 Fiddler 访问 Internet 的防火墙豁免。

    【讨论】:

    • 这似乎是问题所在。我将我的防火墙选项从 Automatic 更改为 Allow Access 现在它可以工作了。为什么Automatic访问会阻止我的程序仍然很奇怪......
    猜你喜欢
    • 1970-01-01
    • 2011-06-10
    • 1970-01-01
    • 2012-05-30
    • 1970-01-01
    • 2017-10-14
    • 2013-07-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多