zoukankan      html  css  js  c++  java
  • HttpClient单例

    public class HttpClientFactory
        {
            private static HttpClient _httpClient = null;

            static HttpClientFactory()
            {
                _httpClient = new HttpClient(new HttpClientHandler());
            }

            public static HttpClient GetHttpClient()
            {
                return _httpClient;
            }


            public static string HttpGet(string url)
            {
                var httpClient = GetHttpClient();
                var response = httpClient.GetAsync(new Uri(url)).Result;
                return response.Content.ReadAsStringAsync().Result;
            }


            public static string HttpGetWithHeaders(string url, string ticket)
            {
                var httpClient = GetHttpClient();
                /tpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Authorization", "BasicAuth " + ticket);
                httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", ticket);
                var response = httpClient.GetAsync(new Uri(url)).Result;
                return response.Content.ReadAsStringAsync().Result;
            }

        }
    }

  • 相关阅读:
    CF997C Sky Full of Stars
    LOJ6160 二分图染色
    AT4996 [AGC034F] RNG and XOR
    AT4119 [ARC096C] Everything on It
    20200701线性代数概率期望练习
    SNOI2020 LOJ3326 字符串
    SNOI2020 LOJ3323 生成树
    SNOI2020 LOJ3324 取石子
    Gym-102576A Bags of Candies
    Gym-102576H Lighthouses
  • 原文地址:https://www.cnblogs.com/bo0214/p/12800703.html
Copyright © 2011-2022 走看看