HttpClient.GetAsync()在Azure Function上获取数据时给出AggregateExcepti



我正试图使用URL:从Zoho获取员工数据

https://people.zoho.com/people/api/forms/P_EmployeeView/records 

使用CCD_ 1的CCD_。在我的本地开发环境中执行代码时,代码运行平稳并获取所需的数据,但一旦我将代码发布到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, Int32 port, 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:ProjectsZohoAttendanceInternal-Automation-and-Power-BI-Dashboard-zoho-employee-data-
updatesrcEmployeeDataRefreshEmployeeDataRefreshZohoClient.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:ProjectsZohoAttendanceInternal-
Automation-and-Power-BI-Dashboard-zoho-employee-data-
updatesrcEmployeeDataRefreshEmployeeDataRefreshTrigger.cs:line 68at 
EmployeeDataRefresh.Trigger.AutoRefreshEmployeeData(TimerInfo myTimer, ILogger log) in 
C:ProjectsZohoAttendanceInternal-Automation-and-Power-BI-Dashboard-zoho-employee-data-
updatesrcEmployeeDataRefreshEmployeeDataRefreshTrigger.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发送Get请求">。我已经打印了令牌和其他必需的变量,以检查它们是否有正确的值,并且它们得到了正确的值——所以无效令牌绝对不是问题。有人能提出这个错误的可能原因吗?

您希望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;
}

最新更新