首页 > 解决方案 > System.AccessViolationException 发生在 System.Net.Http C#

问题描述

我有以下代码:

using (var ct = new Google.Apis.SearchConsole.v1.SearchConsoleService(new BaseClientService.Initializer
 {
    ApiKey = ConfigurationManager.AppSettings["googleSearchApiKey"],
    ApplicationName = "my info"
 }))
 {

    html = ct.HttpClient.GetStringAsync("https://www.google.com/search?q=" + WebUtility.UrlEncode(gameName) + "+origin+pc+game").Result;
 }

 HtmlDocument doc = new HtmlDocument();
 doc.LoadHtml(html);

 var glist = doc.DocumentNode.GetById("search")?.GetByClass("g");
 if (glist != null)
 {
    var off = glist.First();
    var a = off.GetByTag("a").FirstOrDefault();

    if (a != null)
    {
       var url = a.GetAttributeValue("href", "");

       if (!string.IsNullOrWhiteSpace(url))
       {
          url = WebUtility.UrlDecode(url);
          url = url.Substring(url.IndexOf("?q=") + 3);
          url = url.Substring(0, url.IndexOf('&'));

          gameUrl = url;
          return stuff;
       }
    }
 }

到达时ct.HttpClient.GetStringAsync,我得到了漂亮的错误页面:

应用程序:MyApp.exe 框架版本:v4.0.30319 描述:进程因未处理的异常而终止。异常信息: System.Net.Http.HttpClient.SendAsync 处的 System.AccessViolationException(System.Net.Http.HttpRequestMessage,System.Net.Http.HttpCompletionOption,System.Threading.CancellationToken)处 System.Net.Http.HttpClient.GetAsync( System.Uri、System.Net.Http.HttpCompletionOption、System.Threading.CancellationToken)

为什么 ?

标签: c#

解决方案


推荐阅读