Visual Studio 2019,.net 3.0预览,创建了一个大型应用程序。试图从https://api.weather.gov/gridpoints/aly/aly/59,14/forecast获取天气数据。我在C#中使用HTTPCLIENT。这被禁止(403(响应
试图添加CORS策略
private async Task<IWeatherDotGovForecast> RetrieveForecast()
{
string url = @"https://api.weather.gov/gridpoints/ALY/59,14/forecast";
var response = await _httpClient.GetAsync(url);
if (response != null)
{
var jsonString = await response.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<WeatherDotGovForecast>(jsonString);
}
//return await _httpClient.GetJsonAsync<WeatherDotGovForecast>
// ("https://api.weather.gov/gridpoints/ALY/59,14/forecast");
return null;
}
我期望JSON数据从https://api.weather.gov/gridpoints/aly/aly/59,14/forecast
相反,我被禁止(403(状态代码
您的问题与Blazor无关,但Weather.gov在任何HTTP请求中都需要用户代理标头。
应用程序在Weather.gov上访问资源的应用程序现在需要在任何HTTP请求中提供用户代理标头。没有用户代理的请求会自动阻止。由于少数客户利用资源远远超过了大多数人认为合理的资源,我们已经实施了此使用政策。
。使用类似的东西:
var _httpClient = new HttpClient();
string url = @"https://api.weather.gov/gridpoints/ALY/59,14/forecast";
_httpClient.DefaultRequestHeaders.Add("User-Agent", "posterlagerkarte");
var response = await _httpClient.GetAsync(url);