コード例 #1
0
        internal WeixinApiClient(
            HttpClient httpMessageInvoker,
            HttpVersion httpVersion,
            IList <HttpRequestInterceptor> requestInterceptors,
            IList <HttpResponseInterceptor> responseInterceptors)
            : base()
        {
            if (httpMessageInvoker == null)
            {
                throw new ArgumentNullException(nameof(httpMessageInvoker));
            }

            IHttpClientBuilder httpClientBuilder = new BasicHttpClientBuilder()
                                                   .SetVersion(httpVersion)
                                                   .SetHttpMessageInvoker(httpMessageInvoker);

            foreach (HttpRequestInterceptor interceptor in requestInterceptors)
            {
                httpClientBuilder.AddRequestInterceptor(interceptor);
            }

            foreach (HttpResponseInterceptor interceptor in responseInterceptors)
            {
                httpClientBuilder.AddResponseInterceptor(interceptor);
            }

            _httpClient = httpClientBuilder.Build();
            _httpClient.DefaultRequestHeaders.Add("Content-Type", "application/json");
        }
コード例 #2
0
        public async Task ResponseInterceptorTest()
        {
            const string STR_HEADER_NAME  = "X-NonExists-Header";
            const string STR_HEADER_VALUE = "test";

            IHttpClientBuilder httpClientBuilder = new BasicHttpClientBuilder();

            httpClientBuilder.SetBaseUri(new Uri("https://www.baidu.com"));
            httpClientBuilder.SetTimeout(TimeSpan.FromSeconds(10));
            httpClientBuilder.UseUserAgentInterceptor(UserAgents.ChromeOnMacOS);
            httpClientBuilder.AddResponseInterceptor(response =>
            {
                response.Headers[STR_HEADER_NAME] = STR_HEADER_VALUE;
            });

            using (IHttpClient httpClient = httpClientBuilder.Build())
            {
                IHttpResponse response = await httpClient.GetTextAsync("s?wd=helloworld");

                Assert.Equal(STR_HEADER_VALUE, response.Headers[STR_HEADER_NAME]);
            }
        }