HttpClient in .net issues 2 requests when providing username and password in NetworkCredentials

前端 未结 4 1559
误落风尘
误落风尘 2021-01-18 08:00

When using the HttpClient in .net 4.5 to do basic authentication I\'m finding that it\'s issuing 2 requests.

The first fails with a HTTP/1.1 401 Unauthorized and the

4条回答
  •  不思量自难忘°
    2021-01-18 08:13

    You could try setting HttpClientHandler.PreAuthenticate as per Tobberoth's answer, although the documentation for that suggests it will only help after the very first request:

    With the exception of the first request, the PreAuthenticate property indicates whether to send authentication information with subsequent requests to a Uri that matches the specific Uri up to the last forward slash without waiting to be challenged by the server.

    It won't help for the very first request, but it may help to reduce the number of round trips after that.

    Another thing to try is including "Authorization" in HttpClient.DefaultRequestHeaders. I'd be slightly surprised if that worked, but it's worth trying, at least.

提交回复
热议问题