是否可以配置HttpClient不保存cookie?

问题描述 投票:2回答:1

我有一个简单的qazxsw poi Web Api,我使用qazxsw poi发送一些custum web请求。我这样使用Asp.Net Core

HttpClient

我注意到,当我发送多个请求时,HttpClient会保存在services.AddHttpClient<IMyInterface, MyService>() ... public class MyService : IMyInterface { private readonly HttpClient _client; public MyService(HttpClient client) { _client = client; } public async Task SendWebRequest() { var url = "https://MyCustomUrl.com/myCustomResource"; var request = new HttpRequestMessage(HttpMethod.Get, url); var response = await _client.SendAsync(request); ... } } 标头中第一次响应时收到的cookie。它将这些cookie添加到连续的请求标头中。 (我用HttpClient检查过这个)。流:

Set-Cookie

有没有办法强迫fiddler不添加cookie?

这只发生在同一个会话中,所以如果我要处理//First requst GET https://MyCustomUrl.com/myCustomResource HTTP/1.1 //First response HTTP/1.1 200 OK Set-Cookie: key=value ... //Second request GET https://MyCustomUrl.com/myCustomResource HTTP/1.1 Cookie: key=value //Second response HTTP/1.1 200 OK ... 那么就不会添加cookie。但处理HttpClient可能会带来一些其他问题。

c# asp.net-core cookies dotnet-httpclient
1个回答
6
投票

HttpClient

获取或设置一个值,该值指示处理程序是否使用CookieContainer属性存储服务器cookie并在发送请求时使用这些cookie。

所以你需要这样做:

HttpClient

如果你在asp.net核心中使用HttpClientHandler.UseCookies,那么var handler = new HttpClientHandler() { UseCookies = false }; var httpClient = new HttpClient(handler); 建议这样做的方法是:

HttpClientFactory
© www.soinside.com 2019 - 2024. All rights reserved.