gpt4 book ai didi

c# - 如果不打开 Fiddler,为什么在下载页面时会出现 SocketException?

转载 作者:太空宇宙 更新时间:2023-11-03 10:33:38 25 4
gpt4 key购买 nike

我遇到了一个奇怪的情况。我有一种方法可以下载如下页面:

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: Can't connect to remote server ---> System.Net.Sockets.SocketException: An attempt was made to access a socket in a way forbidden by its access permissions xxx.xxx.xxx:80] System.Net.Sockets.Socket.DoConnect(EndPoint 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& address, ConnectSocketState state, IAsyncResult asyncResult, Exception& exception) --- İç özel durum yığını izlemesinin sonu --- konum: System.Net.WebClient.DownloadDataInternal(Uri address, WebRequest& request) konum: System.Net.WebClient.DownloadString(Uri address) konum: System.Net.WebClient.DownloadString(String address) konum: Kokoshome_Product_Downloader.Helpers.SiteHelpers.DownloadPageContent(String url, String& content, Int32& statusCode, CookieContainer cookie, Int32 maxAttempt, Encoding encoding)

奇怪的是,如果我打开 Fiddler2 并运行该程序,它运行良好!这段代码曾经有效,我一直在使用 Fiddler一段时间以来,据我所知,我没有更改任何选项。但是现在出了点问题,我无法弄清楚。那么可能是什么问题呢?

注意:我的互联网连接没有其他问题。我可以在没有 Fiddler 的情况下连接互联网。我的操作系统是 Windows 8.1

更新

当我尝试连接 MySQL 数据库和 FTP 服务器时,我遇到了同样的异常。这也不适用于 Fiddler .我做了一些研究,并在 Windows Sockets Error Codes 中找到了有关错误的信息。

WSAEACCES 10013

Permission denied. An attempt was made to access a socket in a way forbidden by its access permissions. An example is using a broadcast address for sendto without broadcast permission being set using setsockopt(SO_BROADCAST). Another possible reason for the WSAEACCES error is that when the bind function is called (on Windows NT 4.0 with SP4 and later), another application, service, or kernel mode driver is bound to the same address with exclusive access. Such exclusive access is a new feature of Windows NT 4.0 with SP4 and later, and is implemented by using the SO_EXCLUSIVEADDRUSE option.

但我仍然不知道如何修复它。

最佳答案

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

关于c# - 如果不打开 Fiddler,为什么在下载页面时会出现 SocketException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28779955/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com