Example #1
0
        private IHttpClient GetHttpClient()
        {
            IHttpClientBuilder httpClientBuilder = new BasicHttpClientBuilder();

            httpClientBuilder.SetBaseUri(new Uri("https://www.bing.com"));
            httpClientBuilder.SetTimeout(TimeSpan.FromSeconds(10));
            httpClientBuilder.UseUserAgentInterceptor(UserAgents.ChromeOnMacOS);
            httpClientBuilder.UseKeepAliveInterceptor(true);

            return(httpClientBuilder.Build());
        }
        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]);
            }
        }