在 Blazor 服务器端使用 HttpClient 获取 StatusCode 407 AuthenticationRequired
Using HttpClient in Blazor server side getting StatusCode 407 AuthenticationRequired
我正在尝试从 Blazor 服务器端项目访问 WebApi,已按如下方式配置 HttpClient,但我总是得到:
{StatusCode: 407, ReasonPhrase: 'authenticationrequired', Version: 1.1, Content: System.Net.Http.HttpConnectionResponseContent, Headers:{ Date: Thu, 24 Oct 2019 11:01:00 GMT Cache-Control: no-cache X-Frame-Options: deny Proxy-Connection: Keep-Alive Proxy-Authenticate: NTLM Content-Type: text/html Content-Length: 3851}}
services.AddHttpClient<IUsersService, HttpUsersService>(options =>
{
options.BaseAddress = new Uri("https://api.example.com/");
options.DefaultRequestHeaders.Accept.Clear();
options.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
})
.ConfigureHttpMessageHandlerBuilder(h =>
new HttpClientHandler
{
DefaultProxyCredentials = CredentialCache.DefaultCredentials,
});
private async Task<User> GetUser()
{
var response = await _httpClient.GetAsync("v1/Users");
var test = await response.Content.ReadAsStreamAsync();
var model = await JsonSerializer.DeserializeAsync<User[]>(test);
return model.First();
}
有没有办法获取系统的代理和凭据或者我在这里做错了什么?
你在哪里应用这个代码:
services.AddHttpClient<IUsersService, HttpUsersService>(options =>
{
options.BaseAddress = new Uri("https://api.example.com/");
options.DefaultRequestHeaders.Accept.Clear();
options.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
})
.ConfigureHttpMessageHandlerBuilder(h =>
new HttpClientHandler
{
DefaultProxyCredentials = CredentialCache.DefaultCredentials,
});
我猜是 Startup class,对吧?
好的,因为你在上面提到你正在从 Blazor 客户端应用程序调用 Web Api 端点,所以不应使用此代码。我猜,这段代码从 IHttpClientFactory 创建了一个 HttpClient 服务。这与 Blazor 客户端应用程序中使用的 HttpClient 服务无关,该服务基于 JavaScript Fetch API.
但是,Blazor 框架默认将 HttpClient 服务添加到 DI 容器中,要使用它,您只需将其注入到您的组件或 class 服务中...
样本:
@page "/"
@inject HttpClient Http
// more code...
@code {
User[] users;
// more code...
private async Task<User> GetUser()
{
users = await Http.GetJsonAsync<User[]>("v1/Users");
return model.First();
}
}
希望这对您有所帮助...
我正在尝试从 Blazor 服务器端项目访问 WebApi,已按如下方式配置 HttpClient,但我总是得到:
{StatusCode: 407, ReasonPhrase: 'authenticationrequired', Version: 1.1, Content: System.Net.Http.HttpConnectionResponseContent, Headers:{ Date: Thu, 24 Oct 2019 11:01:00 GMT Cache-Control: no-cache X-Frame-Options: deny Proxy-Connection: Keep-Alive Proxy-Authenticate: NTLM Content-Type: text/html Content-Length: 3851}}
services.AddHttpClient<IUsersService, HttpUsersService>(options =>
{
options.BaseAddress = new Uri("https://api.example.com/");
options.DefaultRequestHeaders.Accept.Clear();
options.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
})
.ConfigureHttpMessageHandlerBuilder(h =>
new HttpClientHandler
{
DefaultProxyCredentials = CredentialCache.DefaultCredentials,
});
private async Task<User> GetUser()
{
var response = await _httpClient.GetAsync("v1/Users");
var test = await response.Content.ReadAsStreamAsync();
var model = await JsonSerializer.DeserializeAsync<User[]>(test);
return model.First();
}
有没有办法获取系统的代理和凭据或者我在这里做错了什么?
你在哪里应用这个代码:
services.AddHttpClient<IUsersService, HttpUsersService>(options =>
{
options.BaseAddress = new Uri("https://api.example.com/");
options.DefaultRequestHeaders.Accept.Clear();
options.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
})
.ConfigureHttpMessageHandlerBuilder(h =>
new HttpClientHandler
{
DefaultProxyCredentials = CredentialCache.DefaultCredentials,
});
我猜是 Startup class,对吧?
好的,因为你在上面提到你正在从 Blazor 客户端应用程序调用 Web Api 端点,所以不应使用此代码。我猜,这段代码从 IHttpClientFactory 创建了一个 HttpClient 服务。这与 Blazor 客户端应用程序中使用的 HttpClient 服务无关,该服务基于 JavaScript Fetch API.
但是,Blazor 框架默认将 HttpClient 服务添加到 DI 容器中,要使用它,您只需将其注入到您的组件或 class 服务中...
样本:
@page "/"
@inject HttpClient Http
// more code...
@code {
User[] users;
// more code...
private async Task<User> GetUser()
{
users = await Http.GetJsonAsync<User[]>("v1/Users");
return model.First();
}
}
希望这对您有所帮助...