试图发送HTTP请求时奇怪的例外

发布于 2025-01-26 06:35:35 字数 3685 浏览 1 评论 0原文

我在尝试通过执行HTTP请求来获取网站内容的同时获得此例外。 我尝试了几个不同的URL(包括粘贴),并且我得到了一个奇怪的例外,这些例外是我从未得到过的:

    private static string GetWebsiteContent()
    {
        
        try
        {
            var webClient = new WebClient();
            webClient.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:96.0) Gecko/20100101 Firefox/96.0");

            Console.WriteLine(webClient.DownloadString("https://pastebin.com/MyUrl"));
           

            return null;
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
        }

        return null;
    }

这是我所说的方法,我不需要任何形式的解释,只是对网站的简单请求。

例外:

System.Configuration.ConfigurationErrorsException: Impossibile inizializzare il sistema di configurazione ---> System.Configuration.ConfigurationErrorsException: Errore durante il cari
camento di un file di configurazione: L'inizializzatore di tipo di 'System.MarvinHash' ha generato un'eccezione. (C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Config\machine.config)
 ---> System.TypeInitializationException: L'inizializzatore di tipo di 'System.MarvinHash' ha generato un'eccezione. ---> System.NullReferenceException: Riferimento a un oggetto non im
postato su un'istanza di oggetto.
   in System.Collections.HashHelpers.GetEntropy()
   in System.OrdinalComparer.System.IWellKnownStringEqualityComparer.GetRandomizedEqualityComparer()
   in System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
   in System.Security.Cryptography.CryptoConfig.get_DefaultNameHT()
   in System.Security.Cryptography.CryptoConfig.CreateFromName(String name, Object[] args)
   in System.Security.Cryptography.RandomNumberGenerator.Create()
   in System.MarvinHash.GenerateSeed()
   in System.MarvinHash..cctor()
   --- Fine della traccia dello stack dell'eccezione interna ---
   in System.Xml.NameTable..ctor()
   in System.Configuration.XmlUtil..ctor(Stream stream, String name, Boolean readToFirstElement, ConfigurationSchemaErrors schemaErrors)
   in System.Configuration.BaseConfigurationRecord.InitConfigFromFile()
   --- Fine della traccia dello stack dell'eccezione interna ---
   in System.Configuration.ConfigurationSchemaErrors.ThrowIfErrors(Boolean ignoreLocal)
   in System.Configuration.BaseConfigurationRecord.ThrowIfParseErrors(ConfigurationSchemaErrors schemaErrors)
   in System.Configuration.ClientConfigurationSystem.EnsureInit(String configKey)
   --- Fine della traccia dello stack dell'eccezione interna ---
   in System.Configuration.ClientConfigurationSystem.EnsureInit(String configKey)
   in System.Configuration.ClientConfigurationSystem.PrepareClientConfigSystem(String sectionName)
   in System.Configuration.ClientConfigurationSystem.System.Configuration.Internal.IInternalConfigSystem.GetSection(String sectionName)
   in System.Configuration.ConfigurationManager.GetSection(String sectionName)
   in System.Configuration.PrivilegedConfigurationManager.GetSection(String sectionName)
   in System.Diagnostics.DiagnosticsConfiguration.GetConfigSection()
   in System.Diagnostics.DiagnosticsConfiguration.Initialize()
   in System.Diagnostics.DiagnosticsConfiguration.get_Sources()
   in System.Diagnostics.TraceSource.Initialize()
   in System.Net.Logging.InitializeLogging()
   in System.Net.Logging.get_On()
   in System.Net.WebClient.DownloadString(Uri address)
   in SchoolProject.WebUtils.GetWebsiteContent() in C:\Users\Admin\Desktop\Projects\CS\SchoolProject\WebUtils.cs:riga 25

Process finished with exit code -1,073,741,510.

IM在不同的计算机上尝试了以下代码,但结果仍然相同。

我正在使用.NET 4.0(必须)

有人知道为什么会发生这种情况以及如何解决此问题?

Im getting this exception while trying to get a website content by doing an HTTP request.
I tryed with several different urls (including PasteBin) and im getting a weird exception which i never got before:

    private static string GetWebsiteContent()
    {
        
        try
        {
            var webClient = new WebClient();
            webClient.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:96.0) Gecko/20100101 Firefox/96.0");

            Console.WriteLine(webClient.DownloadString("https://pastebin.com/MyUrl"));
           

            return null;
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
        }

        return null;
    }

Thats the method that i call, i dont it need any kind of explanation, just a simple request to a site.

The exception:

System.Configuration.ConfigurationErrorsException: Impossibile inizializzare il sistema di configurazione ---> System.Configuration.ConfigurationErrorsException: Errore durante il cari
camento di un file di configurazione: L'inizializzatore di tipo di 'System.MarvinHash' ha generato un'eccezione. (C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Config\machine.config)
 ---> System.TypeInitializationException: L'inizializzatore di tipo di 'System.MarvinHash' ha generato un'eccezione. ---> System.NullReferenceException: Riferimento a un oggetto non im
postato su un'istanza di oggetto.
   in System.Collections.HashHelpers.GetEntropy()
   in System.OrdinalComparer.System.IWellKnownStringEqualityComparer.GetRandomizedEqualityComparer()
   in System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
   in System.Security.Cryptography.CryptoConfig.get_DefaultNameHT()
   in System.Security.Cryptography.CryptoConfig.CreateFromName(String name, Object[] args)
   in System.Security.Cryptography.RandomNumberGenerator.Create()
   in System.MarvinHash.GenerateSeed()
   in System.MarvinHash..cctor()
   --- Fine della traccia dello stack dell'eccezione interna ---
   in System.Xml.NameTable..ctor()
   in System.Configuration.XmlUtil..ctor(Stream stream, String name, Boolean readToFirstElement, ConfigurationSchemaErrors schemaErrors)
   in System.Configuration.BaseConfigurationRecord.InitConfigFromFile()
   --- Fine della traccia dello stack dell'eccezione interna ---
   in System.Configuration.ConfigurationSchemaErrors.ThrowIfErrors(Boolean ignoreLocal)
   in System.Configuration.BaseConfigurationRecord.ThrowIfParseErrors(ConfigurationSchemaErrors schemaErrors)
   in System.Configuration.ClientConfigurationSystem.EnsureInit(String configKey)
   --- Fine della traccia dello stack dell'eccezione interna ---
   in System.Configuration.ClientConfigurationSystem.EnsureInit(String configKey)
   in System.Configuration.ClientConfigurationSystem.PrepareClientConfigSystem(String sectionName)
   in System.Configuration.ClientConfigurationSystem.System.Configuration.Internal.IInternalConfigSystem.GetSection(String sectionName)
   in System.Configuration.ConfigurationManager.GetSection(String sectionName)
   in System.Configuration.PrivilegedConfigurationManager.GetSection(String sectionName)
   in System.Diagnostics.DiagnosticsConfiguration.GetConfigSection()
   in System.Diagnostics.DiagnosticsConfiguration.Initialize()
   in System.Diagnostics.DiagnosticsConfiguration.get_Sources()
   in System.Diagnostics.TraceSource.Initialize()
   in System.Net.Logging.InitializeLogging()
   in System.Net.Logging.get_On()
   in System.Net.WebClient.DownloadString(Uri address)
   in SchoolProject.WebUtils.GetWebsiteContent() in C:\Users\Admin\Desktop\Projects\CS\SchoolProject\WebUtils.cs:riga 25

Process finished with exit code -1,073,741,510.

Im tryed the following code on different computers, but the result still the same.

Im using .NET 4.0 (must)

Does anyone know why this happens and how to fix this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北音执念 2025-02-02 06:35:35

不再建议使用WebClient。最好使用httpclient而不是

private static async Task<string> GetWebsiteContent()
{
    try
    {
         var client = new HttpClient();
         //If You Need Add Header 
         //client.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:96.0) Gecko/20100101 Firefox/96.0");       
         var response = await client.GetAsync("https://pastebin.com/url");
         var content = await response.Content.ReadAsStringAsync();
         return content;
     }
     catch (Exception ex)
     {
          Console.WriteLine(ex.ToString());
     }

        return null;
  }

It is no longer recommended to use WebClient. And it is better to use HttpClient instead

private static async Task<string> GetWebsiteContent()
{
    try
    {
         var client = new HttpClient();
         //If You Need Add Header 
         //client.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:96.0) Gecko/20100101 Firefox/96.0");       
         var response = await client.GetAsync("https://pastebin.com/url");
         var content = await response.Content.ReadAsStringAsync();
         return content;
     }
     catch (Exception ex)
     {
          Console.WriteLine(ex.ToString());
     }

        return null;
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文