我得到了一个非常简单的httpClientHandler,它不会从调用基返回。发送异步。
我想做的是获取一个身份验证cookie,以便我知道何时刷新CouchDB身份验证。
没有例外或其他任何内容可以指示导致问题的原因。
建议?
下面是调用类:
public StdHttpClient(IConfiguration conf)
{
configuration = conf;
Client = new HttpClient(new AuthHandler());
}
public StdHttpClient()
{
Client = new HttpClient(new AuthHandler());
}
private async Task<Boolean> GetAuthenticationCookie(Credentials credentials)
{
var authReq = new { name = credentials.username, credentials.password };
var authReqString = JsonConvert.SerializeObject(authReq);
var request = new HttpRequestMessage(HttpMethod.Post, sessionUri);
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
request.Content = new StringContent(authReqString);
request.Content.Headers.ContentType = new
MediaTypeHeaderValue("application/json");
HttpResponseMessage authResult = await Client.SendAsync(request);
Console.WriteLine("GetAuth" + authResult.ToString()); //doesn't get here
return authResult.IsSuccessStatusCode;
}
处理程序是:
public class AuthHandler: DelegatingHandler
{
private readonly int _maximumAmountOfRetries = 3;
public AuthHandler() : base()
{
}
public AuthHandler(HttpMessageHandler innerHandler) : base(innerHandler)
{
}
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request,
CancellationToken cancellationToken)
{
HttpResponseMessage response = null;
response = await base.SendAsync(request, cancellationToken);
Console.WriteLine("got response from sendasync"); //doesn't get here
Console.WriteLine(response);
return response;
}
}
好吧,我很愚蠢。 我试图调用身份验证作为程序启动代码的一部分.cs因此任务是孤立的。
对此感到抱歉。