• 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;
            }

        }
    }

  • 相关阅读:
    WCF 4.0中的动态发现服务WSDiscovery
    Windows Server 2008 R2 Server Core
    Open Source Web Design
    Windows Identity Foundation(WIF)正式发布
    是开始学习IronPython 的时候了
    IronPython 承载和消费WCF服务
    微博客程序 Yonkly
    如何启用匿名访问SQL Server Reporting Service 2008
    ASP.NET MVC 2 RC 发布
    SharePoint 2010 VHD下载
  • 原文地址:https://www.cnblogs.com/bo0214/p/12800703.html
Copyright © 2020-2023  润新知