gpt4 book ai didi

Azure Functions HttpClient 根据请求没有已知这样的主机

转载 作者:行者123 更新时间:2023-12-03 03:40:31 25 4
gpt4 key购买 nike

我正在尝试查看 API 是否已启动,但是当我将代码发布到 Azure 时,HttpClient 无法获取任何内容,因为当它尝试 .SendAsync(); 时,它无法获取任何内容。我从 Azure 收到的错误消息是“No Such Host is Known”,但代码在我的本地环境中确实可以正常工作。

从中您知道什么可能会丢失或导致 HttpClient Get 失败吗?

public class ConnectionTester
{
[FunctionName("ConnectionTester")]
public static async Task Run([TimerTrigger("* */30 * * * *")] TimerInfo connectionTimer, ILogger log)
{
log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");

var client = new HttpClient();
client.DefaultRequestHeaders.Add("Authorization", strAuth);
HttpRequestMessage newRequest = new HttpRequestMessage(HttpMethod.Get, strLink);
var response = await client.SendAsync(newRequest);

if (response.IsSuccessStatusCode)
{
log.LogInformation("Success");
}
else
{
log.LogError("Error");
}
}
}

最佳答案

我遵循了问题中给出的相同代码并复制如下:

堆栈:Azure Functions (.NET 6) - 计时器触发器

Function1.cs

using System;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.Extensions.Logging;

namespace KrishFunctionApp1205
{
public class Function1
{
[FunctionName("Function1")]
public async Task RunAsync([TimerTrigger("0 */1 * * * *")]TimerInfo myTimer, ILogger log)
{
log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
var strLink = "https://stackoverflow.com/";
var strAuth = "Key05";
var client = new HttpClient();
client.DefaultRequestHeaders.Add("Authorization", strAuth);
HttpRequestMessage newRequest = new HttpRequestMessage(HttpMethod.Get, strLink);
var response = await client.SendAsync(newRequest);

if (response.IsSuccessStatusCode)
log.LogInformation("Okidoki");
else
log.LogError($"{response.StatusCode} {response.ReasonPhrase}: ");
}
}
}

<强> local.settings.json :

{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"FUNCTIONS_WORKER_RUNTIME": "dotnet"
}
}

host.json:

{
"version": "2.0",
"logging": {
"applicationInsights": {
"samplingSettings": {
"isEnabled": true,
"excludedTypes": "Request"
}
}
}
}

.csproj:

<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<AzureFunctionsVersion>v4</AzureFunctionsVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.ApplicationInsights.WorkerService" Version="2.15.0" />
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.0.1" />
</ItemGroup>
<ItemGroup>
<None Update="host.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="local.settings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<CopyToPublishDirectory>Never</CopyToPublishDirectory>
</None>
</ItemGroup>
</Project>

本地运行结果:

Result

在 Azure 中运行时的结果:

enter image description here

<小时/>

注意:解决以下错误的几个步骤是:

HttpClient No such host is known on Request

  • 可能是 DNS 问题,请检查 URI 是否正常工作。

  • 您尝试发送到 URI 的请求被公司防火墙或系统防火墙阻止。

  • 更改 Azure Functions 项目中的端口号并运行该函数。 (请参阅 here 了解如何更改 Azure Functions 中的端口号)

  • 如果 Visual Studio 中有可用更新,请升级/更新 .NET 项目 SDK。

  • 检查连接字符串是否指向正确的值。要在本地运行并使用本地存储模拟器,则:

    "AzureWebJobsStorage": "UseDevelopmentStorage=true"

    要使用 Azure 存储帐户并在本地运行该函数,则

    "AzureWebJobsStorage": "<Azure-Storage-Account-Connection-String>"

您可以从 Azure 门户 > 存储帐户 > 访问 key > 连接字符串获取此连接字符串。

<小时/>

关于Azure Functions HttpClient 根据请求没有已知这样的主机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71415622/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com