-
-
Notifications
You must be signed in to change notification settings - Fork 159
/
Copy pathHttpClientFactory.cs
65 lines (54 loc) · 1.87 KB
/
HttpClientFactory.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
using System;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
namespace GoogleApi;
//github.com/ <summary>
//github.com/ Provide a default global httpClient and a factory a factory method
//github.com/ </summary>
public static class HttpClientFactory
{
//github.com/ <summary>
//github.com/ Proxy property that will be used for all requests.
//github.com/ </summary>
public static IWebProxy Proxy { get; set; }
//github.com/ <summary>
//github.com/ Create Default Http Client.
//github.com/ </summary>
//github.com/ <returns>The <see cref="HttpClient"/>.</returns>
public static HttpClient CreateDefaultHttpClient()
{
var httpMessageHandler = HttpClientFactory.GetDefaultHttpClientHandler(HttpClientFactory.Proxy);
var httpClient = new HttpClient(httpMessageHandler);
HttpClientFactory.ConfigureDefaultHttpClient(httpClient);
return httpClient;
}
//github.com/ <summary>
//github.com/ Configure Default Http Client.
//github.com/ </summary>
//github.com/ <param name="httpClient"></param>
public static void ConfigureDefaultHttpClient(HttpClient httpClient)
{
if (httpClient == null)
throw new ArgumentNullException(nameof(httpClient));
httpClient.Timeout = TimeSpan.FromSeconds(30);
httpClient.DefaultRequestHeaders.Accept
.Add(new MediaTypeWithQualityHeaderValue("application/json"));
}
//github.com/ <summary>
//github.com/ Get Default Http Client Handler.
//github.com/ </summary>
//github.com/ <returns>The <see cref="HttpMessageHandler"/>.</returns>
public static HttpClientHandler GetDefaultHttpClientHandler(IWebProxy webProxy = null)
{
var httpClientHandler = new HttpClientHandler
{
Proxy = webProxy
};
if (httpClientHandler.SupportsAutomaticDecompression)
{
httpClientHandler.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
}
return httpClientHandler;
}
}