我有以下代码:
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.AccessViolationException 在System.Net.Http.HttpClient.SendAsync(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)
为什么?