如何解决HttpClient.GetAsync() 在 Azure Function 上获取数据时给出 AggregateException
我正在尝试使用 URL 从 Zoho 获取员工数据:
https://people.zoho.com/people/api/forms/P_EmployeeView/records
使用 HttpClient
的 GetAsync()
。在我的本地开发环境中执行代码时,代码运行流畅并获取所需的数据,但是一旦我将代码发布到 azure 函数,我就会收到以下堆栈跟踪异常:
2021-06-01T06:14:45.870 [Error] System.AggregateException: One or more errors occurred. (One or
more errors occurred. (A connection attempt Failed because the connected party did not properly
respond after a period of time,or established connection Failed because connected host has Failed
to respond.))---> System.AggregateException: One or more errors occurred. (A connection attempt
Failed because the connected party did not properly respond after a period of time,or established
connection Failed because connected host has Failed to respond.)--->
System.Net.Http.HttpRequestException: A connection attempt Failed because the connected party did
not properly respond after a period of time,or established connection Failed because connected
host has Failed to respond.---> System.Net.sockets.socketException (10060): A connection attempt
Failed because the connected party did not properly respond after a period of time,or established
connection Failed because connected host has Failed to respond.at
System.Net.Http.ConnectHelper.ConnectAsync(String host,Int32 port,CancellationToken
cancellationToken)--- End of inner exception stack trace ---at
System.Net.Http.ConnectHelper.ConnectAsync(String host,CancellationToken
cancellationToken)at System.Net.Http.httpconnectionPool.ConnectAsync(HttpRequestMessage request,Boolean allowHttp2,CancellationToken cancellationToken)at
System.Net.Http.httpconnectionPool.CreateHttp11ConnectionAsync(HttpRequestMessage request,CancellationToken cancellationToken)at
System.Net.Http.httpconnectionPool.GethttpconnectionAsync(HttpRequestMessage request,CancellationToken cancellationToken)at
System.Net.Http.httpconnectionPool.SendWithRetryAsync(HttpRequestMessage request,Boolean
doRequestAuth,CancellationToken cancellationToken)at
System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request,CancellationToken
cancellationToken)at System.Net.Http.DiagnosticsHandler.SendAsync(HttpRequestMessage request,CancellationToken cancellationToken)at System.Net.Http.HttpClient.FinishSendAsyncBuffered(Task`1
sendTask,HttpRequestMessage request,CancellationTokenSource cts,Boolean disposeCts)--- End of
inner exception stack trace ---at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean
includeTaskCanceledExceptions)at System.Threading.Tasks.Task`1.GetResultCore(Boolean
waitCompletionNotification)at System.Threading.Tasks.Task`1.get_Result()at
EmployeeDataRefresh.ZohoClient.GetEmployeeData(ILogger log) in
C:\Projects\ZohoAttendance\Internal-Automation-and-Power-BI-Dashboard-zoho-employee-data-
update\src\EmployeeDataRefresh\EmployeeDataRefresh\ZohoClient.cs:line 39--- End of inner exception
stack trace ---at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean
includeTaskCanceledExceptions)at System.Threading.Tasks.Task`1.GetResultCore(Boolean
waitCompletionNotification)at System.Threading.Tasks.Task`1.get_Result()at
EmployeeDataRefresh.Trigger.DoRefresh(ILogger log) in C:\Projects\ZohoAttendance\Internal-
Automation-and-Power-BI-Dashboard-zoho-employee-data-
update\src\EmployeeDataRefresh\EmployeeDataRefresh\Trigger.cs:line 68at
EmployeeDataRefresh.Trigger.AutoRefreshEmployeeData(TimerInfo myTimer,ILogger log) in
C:\Projects\ZohoAttendance\Internal-Automation-and-Power-BI-Dashboard-zoho-employee-data-
update\src\EmployeeDataRefresh\EmployeeDataRefresh\Trigger.cs:line 30
using(var httpClient = new HttpClient())
{
httpClient.DefaultRequestHeaders.Authorization
= new AuthenticationHeaderValue("Bearer",_authToken);
Uri myUri = new Uri(_url,UriKind.Absolute);
var response = httpClient.GetAsync(myUri);
log.Loginformation(_authToken);
log.Loginformation("Sending Get Request to Zoho...\n");
var data = await response.Result.Content.ReadAsstringAsync();
log.Loginformation("Data fetched from Zoho...\n");
var employes = JsonConvert.DeserializeObject<List<Employee>>(data);
return employes;
}
我在行中收到错误
var data = await response.Result.Content.ReadAsstringAsync();
我已经放置了各种日志语句来调试问题,最后打印在 azure 函数日志上的日志语句是“向 Zoho 发送获取请求...”。 我已经打印了令牌和其他必需的变量,以检查它们是否具有正确的值以及它们是否获得了正确的值,因此无效的令牌绝对不是问题。有人可以建议此错误的可能原因是什么吗?
解决方法
你期待 json 响应类型吗?为了最佳实践,您可能需要在 httpclient 的标头中提供
string contentTypeValue = "application/json";
client.DefaultRequestHeaders.Add("Content-Type",contentTypeValue);
还要练习
httpResponse.EnsureSuccessStatusCode(); // throws if not 200-299
在读取结果流之前。
,这里没有令牌或任何身份验证的问题,只需清除异步编程,
首先,
相反,通过获取 response.Result
属性的值,您强制当前线程等待直到异步操作完成,第二个我会推荐
static readonly HttpClient client = new HttpClient();
try
{
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer",_authToken);
Uri myUri = new Uri(_url,UriKind.Absolute);
HttpResponseMessage response = await client.GetAsync(_url);
var data = await response.Result.Content.ReadAsStringAsync();
var employes = JsonConvert.DeserializeObject<List<Employee>>(data);
}
catch (Exception)
{
throw;
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。