Ejemplo n.º 1
0
        /// <summary>
        /// 创建HttpClient对象
        /// </summary>
        /// <param name="httpClient">要实例化的httpClient对象</param>
        internal static void CreateHttpClient(ref HttpClient httpClient)
        {
            if (httpClient != null)
            {
                httpClient.Dispose();
            }
            //添加过滤器
            IHttpFilter filter = new HttpBaseProtocolFilter();

            filter     = new PlugInFilter(filter);
            httpClient = new HttpClient(filter);

            //添加User-Agent
            httpClient.DefaultRequestHeaders.UserAgent.Add(new Windows.Web.Http.Headers.HttpProductInfoHeaderValue("mySample", "v1"));
        }
Ejemplo n.º 2
0
            private void init()
            {
                // HttpClient functionality can be extended by plugging multiple filters together and providing
                // HttpClient with the configured filter pipeline.
                IHttpFilter filter = new HttpBaseProtocolFilter();

                filter          = new PlugInFilter(filter); // Adds a custom header to every request and response message.
                this.httpClient = new HttpClient(filter);

                // 使用谷歌浏览器的用户代理
                string userAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36";

                if (!this.httpClient.DefaultRequestHeaders.UserAgent.TryParseAdd(userAgent))
                {
                    Debug.Fail("Failed to use Chrome User Agent");
                }
            }
Ejemplo n.º 3
0
        internal static void CreateHttpClient(ref HttpClient httpClient)
        {
            if (httpClient != null)
            {
                httpClient.Dispose();
            }

            // HttpClient functionality can be extended by plugging multiple filters together and providing
            // HttpClient with the configured filter pipeline.
            IHttpFilter filter = new HttpBaseProtocolFilter();

            filter     = new PlugInFilter(filter); // Adds a custom header to every request and response message.
            httpClient = new HttpClient(filter);

            // The following line sets a "User-Agent" request header as a default header on the HttpClient instance.
            // Default headers will be sent with every request sent from this HttpClient instance.
            httpClient.DefaultRequestHeaders.UserAgent.Add(new HttpProductInfoHeaderValue("gitme"));
        }