forked from anthonyreilly/NetCoreForce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHttpClientFactory.cs
48 lines (42 loc) · 1.49 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
using System;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Collections.Generic;
using System.Diagnostics;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using NetCoreForce.Client.Serializer;
using NetCoreForce.Client.Models;
namespace NetCoreForce.Client
{
public class HttpClientFactory
{
private const string GZipEncoding = "gzip";
private const string DeflateEncoding = "deflate";
public static HttpClient CreateHttpClient(bool useCompression = true, string proxyUrl = null)
{
var handler = new HttpClientHandler();
if (useCompression && handler.SupportsAutomaticDecompression)
{
handler.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
}
if(!string.IsNullOrEmpty(proxyUrl))
{
handler.Proxy = new CustomProxy(proxyUrl);
}
HttpClient httpClient = new HttpClient(handler);
if (useCompression && handler.SupportsAutomaticDecompression)
{
httpClient.DefaultRequestHeaders.AcceptEncoding.Add(new StringWithQualityHeaderValue(GZipEncoding));
httpClient.DefaultRequestHeaders.AcceptEncoding.Add(new StringWithQualityHeaderValue(DeflateEncoding));
}
return httpClient;
}
}
}