在自定义 HttpClientHandler 中设置 allowautoredirect=false
Posted
技术标签:
【中文标题】在自定义 HttpClientHandler 中设置 allowautoredirect=false【英文标题】:set allowautoredirect= false in custom HttpClientHandler 【发布时间】:2020-07-03 17:56:03 【问题描述】:我有像下面这样的自定义 HttpClientHandler
public class CustomHttpClientHandler : HttpClientHandler
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
var bytes = Encoding.ASCII.GetBytes(await request.Content.ReadAsStringAsync());
request.Headers.Add("RequestSignature", Convert.ToBase64String(bytes));
return await base.SendAsync(request, cancellationToken);
我想在这个 CustomHttpClientHandler 中设置allowautoredirect= false
,我该怎么做才能应用到所有请求?
我是这样使用它的:
var client = new HttpClient(new CustomHttpClientHandler());
https://docs.microsoft.com/en-us/dotnet/api/system.net.http.httpclienthandler.allowautoredirect?view=netframework-4.8
【问题讨论】:
【参考方案1】:你可以在构造函数中设置AllowAutoRedirect
property,像这样
public class CustomHttpClientHandler : HttpClientHandler
public CustomHttpClientHandler() : base()
// setup props
AllowAutoRedirect = true;
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
// your stuff
【讨论】:
以上是关于在自定义 HttpClientHandler 中设置 allowautoredirect=false的主要内容,如果未能解决你的问题,请参考以下文章
如何在自定义 HttpMessageHandler 中设置属性?
在自定义 UITableViewCell 中设置 UIButton 的 contentMode 没有效果