具有UseDefaultCredentials

时间:2018-08-23 14:58:54

标签: asp.net asp.net-web-api singleton httpclient

我一直在尝试使用此处的建议来更新我的API调用 只能有1个HttpClient实例。 https://aspnetmonsters.com/2016/08/2016-08-27-httpclientwrong/#

这很好用,我没有用尽所有端口,但是当我尝试创建HttpClientHandler传递默认凭据时,我的端口又开始用完了。出于安全原因,我的API已设置为使用Windows Auth,因此我需要传递应用程序池凭据才能成功调用。

这是2个代码块

    public static class WebApiCallUtility
    {
        private static HttpClientHandler _handlerNoCred = new HttpClientHandler();
        private static HttpClient _clientNoCred = new HttpClient(_handlerNoCred);

        private static HttpClientHandler _handlerCred = new HttpClientHandler { UseDefaultCredentials = true };
        private static HttpClient _clientCred = new HttpClient(_handlerCred);

        //Working ports are not used up
        public static HttpResponseMessage SendHttpGetRequestNoCred(string webApiUrl, string logSourceName, string subId)
        {

            _clientNoCred.DefaultRequestHeaders.Accept.Clear();
            _clientNoCred.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            HttpResponseMessage result = _clientNoCred.GetAsync(webApiUrl).Result;

            return result;

        }

        //No working tons of ports open hanging out with TIME_WAIT status
        public static HttpResponseMessage SendHttpGetRequestCred(string webApiUrl, string logSourceName, string subId)
        {

            _clientCred.DefaultRequestHeaders.Accept.Clear();
            _clientCred.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            HttpResponseMessage result = _clientCred.GetAsync(webApiUrl).Result;

            return result;

        }
    }

任何帮助将不胜感激。

谢谢

1 个答案:

答案 0 :(得分:0)

我能够使用其他处理程序类型来解决此问题的购买

    private static WebRequestHandler _handlerCred = new WebRequestHandler
    {
        UseDefaultCredentials = true,
        UnsafeAuthenticatedConnectionSharing = true
    };

    private static HttpClient _clientCred = new HttpClient(_handlerCred);

我在https://docs.djangoproject.com/en/dev/ref/models/querysets/#get-or-create

处找到了这个答案