在 .net core 使用 httpclient 时,可能会有会话 cookie 被保存的情况处理

在调用其它接口时发现,请求过程的 Cookie 被保存了下来,但是其实是不想要的

似乎在 .net core 的 httpclient 请求机制中,是有 HttpClientHandler 缓存的机制

这时候需要修改 HttpClientHandler 的配置

方法1,直接创建新的 HttpClient :

var handler = new HttpClientHandler() { UseCookies = false };
var httpClient = new HttpClient(handler);

方法2,通过 IHttpClientFactory 创建:

services.AddHttpClient("configured-inner-handler")
    .ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler() { UseCookies = false });

参考:https://stackoverflow.com/questions/55493745/is-it-possible-to-configure-httpclient-not-to-save-cookies

更多:

https://www.cnblogs.com/edison0621/p/11224890.html

https://www.cnblogs.com/edison0621/p/11261373.html

https://www.cnblogs.com/edison0621/p/11298882.html

posted @ 2020-11-04 15:48  不是豆豆  阅读(896)  评论(0编辑  收藏  举报
友情链接:迷途