-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpAgent.cs
More file actions
167 lines (140 loc) · 5.94 KB
/
HttpAgent.cs
File metadata and controls
167 lines (140 loc) · 5.94 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
namespace SharpKit;
public sealed class HttpAgent : IDisposable
{
private readonly HttpClient _client;
private readonly HttpClientHandler _handler;
private bool _disposed;
public HttpAgent()
{
_handler = new HttpClientHandler
{
AllowAutoRedirect = true,
MaxAutomaticRedirections = 10,
UseCookies = true,
ServerCertificateCustomValidationCallback = (_, _, _, _) => true
};
_client = new HttpClient(_handler);
}
public HttpAgent(string proxyUri) : this()
{
_handler.UseProxy = true;
_handler.Proxy = new WebProxy(proxyUri, false);
}
public HttpAgent(string proxyUri, string username, string password) : this()
{
_handler.UseProxy = true;
_handler.Proxy = new WebProxy(proxyUri, false)
{
Credentials = new NetworkCredential(username, password)
};
}
public void SetBasicAuth(string username, string password)
{
var encoded = Convert.ToBase64String(Encoding.UTF8.GetBytes($"{username}:{password}"));
_client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", encoded);
}
public void SetBearerToken(string token)
{
_client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
}
public void SetNtlmAuth(string username, string password, string domain)
{
_handler.Credentials = new NetworkCredential(username, password, domain);
_handler.PreAuthenticate = true;
}
public void SetHeader(string name, string value)
{
_client.DefaultRequestHeaders.Remove(name);
_client.DefaultRequestHeaders.TryAddWithoutValidation(name, value);
}
public void SetUserAgent(string userAgent)
{
_client.DefaultRequestHeaders.Remove("User-Agent");
_client.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", userAgent);
}
public void SetTimeout(TimeSpan timeout)
{
_client.Timeout = timeout;
}
public async Task<HttpResponseMessage> GetAsync(string url, CancellationToken ct = default)
{
return await _client.GetAsync(url, ct).ConfigureAwait(false);
}
public async Task<string> GetStringAsync(string url, CancellationToken ct = default)
{
return await _client.GetStringAsync(url, ct).ConfigureAwait(false);
}
public async Task<byte[]> GetBytesAsync(string url, CancellationToken ct = default)
{
return await _client.GetByteArrayAsync(url, ct).ConfigureAwait(false);
}
public async Task<HttpResponseMessage> PostJsonAsync(string url, string json, CancellationToken ct = default)
{
var content = new StringContent(json, Encoding.UTF8, "application/json");
return await _client.PostAsync(url, content, ct).ConfigureAwait(false);
}
public async Task<HttpResponseMessage> PostFormAsync(string url, Dictionary<string, string> fields, CancellationToken ct = default)
{
var content = new FormUrlEncodedContent(fields);
return await _client.PostAsync(url, content, ct).ConfigureAwait(false);
}
public async Task<HttpResponseMessage> PostBytesAsync(string url, byte[] data, string contentType = "application/octet-stream", CancellationToken ct = default)
{
var content = new ByteArrayContent(data);
content.Headers.ContentType = new MediaTypeHeaderValue(contentType);
return await _client.PostAsync(url, content, ct).ConfigureAwait(false);
}
public async Task<HttpResponseMessage> PutJsonAsync(string url, string json, CancellationToken ct = default)
{
var content = new StringContent(json, Encoding.UTF8, "application/json");
return await _client.PutAsync(url, content, ct).ConfigureAwait(false);
}
public async Task<HttpResponseMessage> DeleteAsync(string url, CancellationToken ct = default)
{
return await _client.DeleteAsync(url, ct).ConfigureAwait(false);
}
public async Task<HttpResponseMessage> SendRawAsync(HttpRequestMessage request, CancellationToken ct = default)
{
return await _client.SendAsync(request, ct).ConfigureAwait(false);
}
public async Task<byte[]> DownloadAsync(string url, IProgress<double>? progress = null, CancellationToken ct = default)
{
using var response = await _client.GetAsync(url, HttpCompletionOption.ResponseHeadersRead, ct).ConfigureAwait(false);
response.EnsureSuccessStatusCode();
var totalBytes = response.Content.Headers.ContentLength ?? -1L;
using var stream = await response.Content.ReadAsStreamAsync(ct).ConfigureAwait(false);
using var ms = new MemoryStream();
var buffer = new byte[81920];
long bytesRead = 0;
int read;
while ((read = await stream.ReadAsync(buffer, ct).ConfigureAwait(false)) > 0)
{
await ms.WriteAsync(buffer.AsMemory(0, read), ct).ConfigureAwait(false);
bytesRead += read;
if (progress != null && totalBytes > 0)
progress.Report((double)bytesRead / totalBytes);
}
return ms.ToArray();
}
public async Task<Dictionary<string, string>> GetResponseHeadersAsync(string url, CancellationToken ct = default)
{
using var response = await _client.GetAsync(url, HttpCompletionOption.ResponseHeadersRead, ct).ConfigureAwait(false);
var headers = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
foreach (var header in response.Headers)
headers[header.Key] = string.Join(", ", header.Value);
foreach (var header in response.Content.Headers)
headers[header.Key] = string.Join(", ", header.Value);
return headers;
}
public void Dispose()
{
if (_disposed) return;
_disposed = true;
_client.Dispose();
_handler.Dispose();
}
}