处理 Azure Function 中的 HttpStatus 302
Deal with HttpStatus 302 in Azure Function
在 Azure Function 中,我正在尝试 return 来自 MS Graph Converter (Doc)
的 pdf 文件
文件 return 处于 302 Http 状态,在本地没什么大不了的,一切正常,但是当我在 azure 函数上发布时我遇到了这个错误:
502 - Web server received an invalid response while acting as a
gateway or proxy server. There is a problem with the page you are
looking for, and it cannot be displayed. When the Web server (while
acting as a gateway or proxy) contacted the upstream content server,
it received an invalid response from the content server.
我的代码:
IConfidentialClientApplication confidentialClientApplication = ConfidentialClientApplicationBuilder
.Create("")
.WithTenantId("")
.WithClientSecret("")
.Build();
ClientCredentialProvider authProvider = new ClientCredentialProvider(confidentialClientApplication);
var graphClient = new GraphServiceClient(authProvider);
var token = await authProvider.ClientApplication.AcquireTokenForClient(new string[] { "https://graph.microsoft.com/.default" }).ExecuteAsync();
HttpClient httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token.AccessToken);
try
{
var response = await httpClient.GetAsync($"https://graph.microsoft.com/v1.0/drives/{Drive_Id}/items/{Item_Id}/content?format=pdf");
log.LogInformation($"Status Code : {response.StatusCode}");
if (response.StatusCode == System.Net.HttpStatusCode.Redirect)
{
var bytes = await httpClient.GetByteArrayAsync(response.Headers.Location);
return new FileContentResult(bytes, "application/pdf");
}
if (response.StatusCode == System.Net.HttpStatusCode.OK)
{
var bytes = await response.Content.ReadAsByteArrayAsync();
return new FileContentResult(bytes, "application/pdf");
}
}
catch (Exception ex)
{
log.LogInformation($"Error : {ex.Message}");
}
return new NotFoundResult();
我认为有某种配置允许 302 临时重定向或类似的东西..
任何帮助都将不胜感激
我发现我的 pb 正在上传文件,流关闭得太早,导致函数退出...
在 Azure Function 中,我正在尝试 return 来自 MS Graph Converter (Doc)
的 pdf 文件文件 return 处于 302 Http 状态,在本地没什么大不了的,一切正常,但是当我在 azure 函数上发布时我遇到了这个错误:
502 - Web server received an invalid response while acting as a gateway or proxy server. There is a problem with the page you are looking for, and it cannot be displayed. When the Web server (while acting as a gateway or proxy) contacted the upstream content server, it received an invalid response from the content server.
我的代码:
IConfidentialClientApplication confidentialClientApplication = ConfidentialClientApplicationBuilder
.Create("")
.WithTenantId("")
.WithClientSecret("")
.Build();
ClientCredentialProvider authProvider = new ClientCredentialProvider(confidentialClientApplication);
var graphClient = new GraphServiceClient(authProvider);
var token = await authProvider.ClientApplication.AcquireTokenForClient(new string[] { "https://graph.microsoft.com/.default" }).ExecuteAsync();
HttpClient httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token.AccessToken);
try
{
var response = await httpClient.GetAsync($"https://graph.microsoft.com/v1.0/drives/{Drive_Id}/items/{Item_Id}/content?format=pdf");
log.LogInformation($"Status Code : {response.StatusCode}");
if (response.StatusCode == System.Net.HttpStatusCode.Redirect)
{
var bytes = await httpClient.GetByteArrayAsync(response.Headers.Location);
return new FileContentResult(bytes, "application/pdf");
}
if (response.StatusCode == System.Net.HttpStatusCode.OK)
{
var bytes = await response.Content.ReadAsByteArrayAsync();
return new FileContentResult(bytes, "application/pdf");
}
}
catch (Exception ex)
{
log.LogInformation($"Error : {ex.Message}");
}
return new NotFoundResult();
我认为有某种配置允许 302 临时重定向或类似的东西..
任何帮助都将不胜感激
我发现我的 pb 正在上传文件,流关闭得太早,导致函数退出...