wasSharp – Diff between revs 14 and 17
?pathlinks?
Rev 14 | Rev 17 | |||
---|---|---|---|---|
Line 9... | Line 9... | |||
9 | using System.Net; |
9 | using System.Net; |
|
10 | using System.Net.Http; |
10 | using System.Net.Http; |
|
11 | using System.Net.Http.Headers; |
11 | using System.Net.Http.Headers; |
|
12 | using System.Text; |
12 | using System.Text; |
|
13 | using System.Threading.Tasks; |
13 | using System.Threading.Tasks; |
|
- | 14 | using wasSharp.Collections.Utilities; |
||
Line 14... | Line 15... | |||
14 | |
15 | |
|
15 | namespace wasSharp.Web |
16 | namespace wasSharp.Web |
|
16 | { |
17 | { |
|
17 | /////////////////////////////////////////////////////////////////////////// |
18 | /////////////////////////////////////////////////////////////////////////// |
|
Line 60... | Line 61... | |||
60 | HTTPClient.DefaultRequestHeaders.UserAgent.Add(userAgent); |
61 | HTTPClient.DefaultRequestHeaders.UserAgent.Add(userAgent); |
|
61 | if (authentication != null) |
62 | if (authentication != null) |
|
62 | { |
63 | { |
|
63 | HTTPClient.DefaultRequestHeaders.Authorization = authentication; |
64 | HTTPClient.DefaultRequestHeaders.Authorization = authentication; |
|
64 | } |
65 | } |
|
- | 66 | // Add some standard headers: |
||
- | 67 | // Accept - for socially acceptable security of mod_sec |
||
- | 68 | // Accept-Encoding - since we want to use compression if possible |
||
65 | if (headers != null) |
69 | switch (headers != null) |
|
- | 70 | { |
||
- | 71 | case false: |
||
- | 72 | headers = new Dictionary<string, string> {{"Accept", @"*/*"}, {"Accept-Encoding", "gzip,defalate"}}; |
||
- | 73 | break; |
||
- | 74 | default: |
||
- | 75 | if (!headers.ContainsKey("Accept")) |
||
- | 76 | { |
||
- | 77 | headers.Add("Accept", @"*/*"); |
||
- | 78 | } |
||
- | 79 | if (!headers.ContainsKey("Accept-Encoding")) |
||
- | 80 | { |
||
- | 81 | headers.Add("Accept-Encoding", "gzip,deflate"); |
||
- | 82 | } |
||
- | 83 | break; |
||
- | 84 | } |
||
- | 85 | foreach (var header in headers) |
||
66 | { |
86 | { |
|
67 | foreach (var header in headers) |
- | ||
68 | { |
- | ||
69 | HTTPClient.DefaultRequestHeaders.Add(header.Key, header.Value); |
87 | HTTPClient.DefaultRequestHeaders.Add(header.Key, header.Value); |
|
70 | } |
- | ||
71 | } |
88 | } |
|
72 | HTTPClient.Timeout = TimeSpan.FromMilliseconds(timeout); |
89 | HTTPClient.Timeout = TimeSpan.FromMilliseconds(timeout); |
|
73 | MediaType = mediaType; |
90 | MediaType = mediaType; |
|
74 | } |
91 | } |
|
Line 98... | Line 115... | |||
98 | { |
115 | { |
|
99 | foreach (var header in headers) |
116 | foreach (var header in headers) |
|
100 | { |
117 | { |
|
101 | request.Headers.Add(header.Key, header.Value); |
118 | request.Headers.Add(header.Key, header.Value); |
|
102 | } |
119 | } |
|
- | 120 | // Add some standard headers: |
||
- | 121 | // Accept - for socially acceptable security of mod_sec |
||
- | 122 | // Accept-Encoding - since we want to use compression if possible |
||
- | 123 | if (!headers.ContainsKey("Accept")) |
||
- | 124 | { |
||
- | 125 | headers.Add("Accept", @"*/*"); |
||
- | 126 | } |
||
- | 127 | if (!headers.ContainsKey("Accept-Encoding")) |
||
- | 128 | { |
||
- | 129 | headers.Add("Accept-Encoding", "gzip,deflate"); |
||
- | 130 | } |
||
103 | using (var response = await HTTPClient.SendAsync(request)) |
131 | using (var response = await HTTPClient.SendAsync(request)) |
|
104 | { |
132 | { |
|
105 | return response.IsSuccessStatusCode |
133 | return response.IsSuccessStatusCode |
|
106 | ? await response.Content.ReadAsByteArrayAsync() |
134 | ? await response.Content.ReadAsByteArrayAsync() |
|
107 | : null; |
135 | : null; |
|
Line 141... | Line 169... | |||
141 | { |
169 | { |
|
142 | foreach (var header in headers) |
170 | foreach (var header in headers) |
|
143 | { |
171 | { |
|
144 | request.Headers.Add(header.Key, header.Value); |
172 | request.Headers.Add(header.Key, header.Value); |
|
145 | } |
173 | } |
|
- | 174 | // Add some standard headers: |
||
- | 175 | // Accept - for socially acceptable security of mod_sec |
||
- | 176 | // Accept-Encoding - since we want to use compression if possible |
||
- | 177 | if (!headers.ContainsKey("Accept")) |
||
- | 178 | { |
||
- | 179 | headers.Add("Accept", @"*/*"); |
||
- | 180 | } |
||
- | 181 | if (!headers.ContainsKey("Accept-Encoding")) |
||
- | 182 | { |
||
- | 183 | headers.Add("Accept-Encoding", "gzip,deflate"); |
||
- | 184 | } |
||
146 | using (var response = await HTTPClient.SendAsync(request)) |
185 | using (var response = await HTTPClient.SendAsync(request)) |
|
147 | { |
186 | { |
|
148 | return response.IsSuccessStatusCode |
187 | return response.IsSuccessStatusCode |
|
149 | ? await response.Content.ReadAsByteArrayAsync() |
188 | ? await response.Content.ReadAsByteArrayAsync() |
|
150 | : null; |
189 | : null; |
|
Line 290... | Line 329... | |||
290 | } |
329 | } |
|
291 | } |
330 | } |
|
Line 292... | Line 331... | |||
292 | |
331 | |
|
293 | public void Dispose() |
332 | public void Dispose() |
|
294 | { |
- | ||
295 | if (HTTPClient != null) |
333 | { |
|
296 | HTTPClient.Dispose(); |
334 | HTTPClient?.Dispose(); |
|
297 | } |
335 | } |
|
298 | } |
336 | } |
|
299 | } |
337 | } |