如何在C#

时间:2018-08-27 10:14:42

标签: c# response-headers

以下代码对我来说工作正常,我使用此httpclient将请求发送到端点。在响应头中,我获取了后续呼叫的位置uri。我想使用相同的httpclient,因为基本uri在位置uri中仍然相同(只是资源不同)。我将如何处理以便可以利用httpclient的好处作为静态实例。

 private static HttpClient client;
    private static object syncLock = new object();
    public static HttpClient GetHttpClient(IApiConfiguration apiConfiguration)
    {
        if (client == null)
        {
            lock (syncLock)
            {
                if (client == null)
                {
                    client = new HttpClient();

                    client.DefaultRequestHeaders.ConnectionClose = false;
                    client.Timeout = TimeSpan.FromMilliseconds(apiConfiguration.Timeout);

                    client.BaseAddress = new Uri(apiConfiguration.BaseUri);

                    client.DefaultRequestHeaders.Accept.Add(
                        new MediaTypeWithQualityHeaderValue("application/json"));
                   ServicePointManager.FindServicePoint(client.BaseAddress).ConnectionLeaseTimeout = apiConfiguration.ConnectionLeaseTimeoutinMilliSec;
                }
            }
        }
        return client;
    }

0 个答案:

没有答案