wasSharp – Diff between revs 48 and 54
?pathlinks?
Rev 48 | Rev 54 | |||
---|---|---|---|---|
1 | /////////////////////////////////////////////////////////////////////////// |
1 | /////////////////////////////////////////////////////////////////////////// |
|
2 | // Copyright (C) Wizardry and Steamworks 2013 - License: GNU GPLv3 // |
2 | // Copyright (C) Wizardry and Steamworks 2013 - License: GNU GPLv3 // |
|
3 | // Please see: http://www.gnu.org/licenses/gpl.html for legal details, // |
3 | // Please see: http://www.gnu.org/licenses/gpl.html for legal details, // |
|
4 | // rights of fair usage, the disclaimer and warranty conditions. // |
4 | // rights of fair usage, the disclaimer and warranty conditions. // |
|
5 | /////////////////////////////////////////////////////////////////////////// |
5 | /////////////////////////////////////////////////////////////////////////// |
|
6 | |
6 | |
|
7 | using System; |
7 | using System; |
|
8 | using System.Collections.Generic; |
8 | using System.Collections.Generic; |
|
9 | using System.IO; |
9 | using System.IO; |
|
10 | using System.Net; |
10 | using System.Net; |
|
11 | using System.Net.Http; |
11 | using System.Net.Http; |
|
12 | using System.Net.Http.Headers; |
12 | using System.Net.Http.Headers; |
|
13 | using System.Text; |
13 | using System.Text; |
|
14 | using System.Threading.Tasks; |
14 | using System.Threading.Tasks; |
|
15 | |
15 | |
|
16 | namespace wasSharp.Web |
16 | namespace wasSharp.Web |
|
17 | { |
17 | { |
|
18 | /////////////////////////////////////////////////////////////////////////// |
18 | /////////////////////////////////////////////////////////////////////////// |
|
19 | // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // |
19 | // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // |
|
20 | /////////////////////////////////////////////////////////////////////////// |
20 | /////////////////////////////////////////////////////////////////////////// |
|
21 | // <summary>A portable HTTP client.</summar> |
21 | // <summary>A portable HTTP client.</summar> |
|
22 | public class wasHTTPClient : IDisposable |
22 | public class wasHTTPClient : IDisposable |
|
23 | { |
23 | { |
|
24 | private HttpClient HTTPClient; |
24 | private HttpClient HTTPClient; |
|
25 | private readonly string MediaType; |
25 | private readonly string MediaType; |
|
26 | |
26 | |
|
27 | public wasHTTPClient(ProductInfoHeaderValue userAgent, CookieContainer cookieContainer, string mediaType, |
27 | public wasHTTPClient(ProductInfoHeaderValue userAgent, CookieContainer cookieContainer, string mediaType, |
|
28 | uint timeout) : this(userAgent, cookieContainer, mediaType, null, null, timeout) |
28 | uint timeout) : this(userAgent, cookieContainer, mediaType, null, null, timeout) |
|
29 | { |
29 | { |
|
30 | } |
30 | } |
|
31 | |
31 | |
|
32 | public wasHTTPClient(ProductInfoHeaderValue userAgent, string mediaType, uint timeout) |
32 | public wasHTTPClient(ProductInfoHeaderValue userAgent, string mediaType, uint timeout) |
|
33 | : this(userAgent, new CookieContainer(), mediaType, null, null, timeout) |
33 | : this(userAgent, new CookieContainer(), mediaType, null, null, timeout) |
|
34 | { |
34 | { |
|
35 | } |
35 | } |
|
36 | |
36 | |
|
37 | public wasHTTPClient(ProductInfoHeaderValue userAgent, string mediaType) |
37 | public wasHTTPClient(ProductInfoHeaderValue userAgent, string mediaType) |
|
38 | : this(userAgent, new CookieContainer(), mediaType, null, null, 60000) |
38 | : this(userAgent, new CookieContainer(), mediaType, null, null, 60000) |
|
39 | { |
39 | { |
|
40 | } |
40 | } |
|
- | 41 | |
||
- | 42 | public wasHTTPClient(ProductInfoHeaderValue userAgent, CookieContainer cookieContainer, string mediaType) |
||
- | 43 | : this(userAgent, new CookieContainer(), mediaType, null, null, 60000) |
||
- | 44 | { |
||
- | 45 | } |
||
41 | |
46 | |
|
42 | public wasHTTPClient(ProductInfoHeaderValue userAgent, CookieContainer cookieContainer, string mediaType, |
47 | public wasHTTPClient(ProductInfoHeaderValue userAgent, CookieContainer cookieContainer, string mediaType, |
|
43 | AuthenticationHeaderValue authentication, Dictionary<string, string> headers, uint timeout) |
48 | AuthenticationHeaderValue authentication, Dictionary<string, string> headers, uint timeout) |
|
44 | { |
49 | { |
|
45 | var HTTPClientHandler = new HttpClientHandler |
50 | var HTTPClientHandler = new HttpClientHandler |
|
46 | { |
51 | { |
|
47 | CookieContainer = cookieContainer, |
52 | CookieContainer = cookieContainer, |
|
48 | UseCookies = true |
53 | UseCookies = true |
|
49 | }; |
54 | }; |
|
50 | if (HTTPClientHandler.SupportsRedirectConfiguration) |
55 | if (HTTPClientHandler.SupportsRedirectConfiguration) |
|
51 | { |
56 | { |
|
52 | HTTPClientHandler.AllowAutoRedirect = true; |
57 | HTTPClientHandler.AllowAutoRedirect = true; |
|
53 | } |
58 | } |
|
54 | if (HTTPClientHandler.SupportsProxy) |
59 | if (HTTPClientHandler.SupportsProxy) |
|
55 | { |
60 | { |
|
56 | HTTPClientHandler.Proxy = WebRequest.DefaultWebProxy; |
61 | HTTPClientHandler.Proxy = WebRequest.DefaultWebProxy; |
|
57 | HTTPClientHandler.UseProxy = true; |
62 | HTTPClientHandler.UseProxy = true; |
|
58 | } |
63 | } |
|
59 | if (HTTPClientHandler.SupportsAutomaticDecompression) |
64 | if (HTTPClientHandler.SupportsAutomaticDecompression) |
|
60 | { |
65 | { |
|
61 | HTTPClientHandler.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate; |
66 | HTTPClientHandler.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate; |
|
62 | } |
67 | } |
|
63 | HTTPClientHandler.ClientCertificateOptions = ClientCertificateOption.Automatic; |
68 | HTTPClientHandler.ClientCertificateOptions = ClientCertificateOption.Automatic; |
|
64 | |
69 | |
|
65 | HTTPClient = new HttpClient(HTTPClientHandler, false); |
70 | HTTPClient = new HttpClient(HTTPClientHandler, false); |
|
66 | HTTPClient.DefaultRequestHeaders.UserAgent.Add(userAgent); |
71 | HTTPClient.DefaultRequestHeaders.UserAgent.Add(userAgent); |
|
67 | if (authentication != null) |
72 | if (authentication != null) |
|
68 | { |
73 | { |
|
69 | HTTPClient.DefaultRequestHeaders.Authorization = authentication; |
74 | HTTPClient.DefaultRequestHeaders.Authorization = authentication; |
|
70 | } |
75 | } |
|
71 | // Add some standard headers: |
76 | // Add some standard headers: |
|
72 | // Accept - for socially acceptable security of mod_sec |
77 | // Accept - for socially acceptable security of mod_sec |
|
73 | switch (headers != null) |
78 | switch (headers != null) |
|
74 | { |
79 | { |
|
75 | case false: |
80 | case false: |
|
76 | headers = new Dictionary<string, string> { { "Accept", @"*/*" } }; |
81 | headers = new Dictionary<string, string> { { "Accept", @"*/*" } }; |
|
77 | break; |
82 | break; |
|
78 | |
83 | |
|
79 | default: |
84 | default: |
|
80 | if (!headers.ContainsKey("Accept")) |
85 | if (!headers.ContainsKey("Accept")) |
|
81 | { |
86 | { |
|
82 | headers.Add("Accept", @"*/*"); |
87 | headers.Add("Accept", @"*/*"); |
|
83 | } |
88 | } |
|
84 | break; |
89 | break; |
|
85 | } |
90 | } |
|
86 | foreach (var header in headers) |
91 | foreach (var header in headers) |
|
87 | { |
92 | { |
|
88 | HTTPClient.DefaultRequestHeaders.Add(header.Key, header.Value); |
93 | HTTPClient.DefaultRequestHeaders.Add(header.Key, header.Value); |
|
89 | } |
94 | } |
|
90 | HTTPClient.Timeout = TimeSpan.FromMilliseconds(timeout); |
95 | HTTPClient.Timeout = TimeSpan.FromMilliseconds(timeout); |
|
91 | MediaType = mediaType; |
96 | MediaType = mediaType; |
|
92 | } |
97 | } |
|
93 | |
- | ||
94 | public void Dispose() |
- | ||
95 | { |
- | ||
96 | HTTPClient?.Dispose(); |
- | ||
97 | HTTPClient = null; |
- | ||
98 | } |
- | ||
99 | |
98 | |
|
100 | /////////////////////////////////////////////////////////////////////////// |
99 | /////////////////////////////////////////////////////////////////////////// |
|
101 | // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // |
100 | // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // |
|
102 | /////////////////////////////////////////////////////////////////////////// |
101 | /////////////////////////////////////////////////////////////////////////// |
|
103 | /// <summary> |
102 | /// <summary> |
|
104 | /// Sends a PUT request to an URL with binary data. |
103 | /// Sends a PUT request to an URL with binary data. |
|
105 | /// </summary> |
104 | /// </summary> |
|
106 | /// <param name="URL">the url to send the message to</param> |
105 | /// <param name="URL">the url to send the message to</param> |
|
107 | /// <param name="data">key-value pairs to send</param> |
106 | /// <param name="data">key-value pairs to send</param> |
|
108 | /// <param name="headers">headers to send with the request</param> |
107 | /// <param name="headers">headers to send with the request</param> |
|
109 | public async Task<byte[]> PUT(string URL, byte[] data, Dictionary<string, string> headers) |
108 | public async Task<byte[]> PUT(string URL, byte[] data, Dictionary<string, string> headers) |
|
110 | { |
109 | { |
|
111 | try |
110 | try |
|
112 | { |
111 | { |
|
113 | using (var content = new ByteArrayContent(data)) |
112 | using (var content = new ByteArrayContent(data)) |
|
114 | { |
113 | { |
|
115 | using ( |
114 | using ( |
|
116 | var request = new HttpRequestMessage |
115 | var request = new HttpRequestMessage |
|
117 | { |
116 | { |
|
118 | RequestUri = new Uri(URL), |
117 | RequestUri = new Uri(URL), |
|
119 | Method = HttpMethod.Put, |
118 | Method = HttpMethod.Put, |
|
120 | Content = content |
119 | Content = content |
|
121 | }) |
120 | }) |
|
122 | { |
121 | { |
|
123 | foreach (var header in headers) |
122 | foreach (var header in headers) |
|
124 | { |
123 | { |
|
125 | request.Headers.Add(header.Key, header.Value); |
124 | request.Headers.Add(header.Key, header.Value); |
|
126 | } |
125 | } |
|
127 | // Add some standard headers: |
126 | // Add some standard headers: |
|
128 | // Accept - for socially acceptable security of mod_sec |
127 | // Accept - for socially acceptable security of mod_sec |
|
129 | if (!headers.ContainsKey("Accept")) |
128 | if (!headers.ContainsKey("Accept")) |
|
130 | { |
129 | { |
|
131 | headers.Add("Accept", @"*/*"); |
130 | headers.Add("Accept", @"*/*"); |
|
132 | } |
131 | } |
|
133 | using (var response = await HTTPClient.SendAsync(request)) |
132 | using (var response = await HTTPClient.SendAsync(request)) |
|
134 | { |
133 | { |
|
135 | return response.IsSuccessStatusCode |
134 | return response.IsSuccessStatusCode |
|
136 | ? await response.Content.ReadAsByteArrayAsync() |
135 | ? await response.Content.ReadAsByteArrayAsync() |
|
137 | : null; |
136 | : null; |
|
138 | } |
137 | } |
|
139 | } |
138 | } |
|
140 | } |
139 | } |
|
141 | } |
140 | } |
|
142 | catch (Exception) |
141 | catch (Exception) |
|
143 | { |
142 | { |
|
144 | return null; |
143 | return null; |
|
145 | } |
144 | } |
|
146 | } |
145 | } |
|
147 | |
146 | |
|
148 | /////////////////////////////////////////////////////////////////////////// |
147 | /////////////////////////////////////////////////////////////////////////// |
|
149 | // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // |
148 | // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // |
|
150 | /////////////////////////////////////////////////////////////////////////// |
149 | /////////////////////////////////////////////////////////////////////////// |
|
151 | /// <summary> |
150 | /// <summary> |
|
152 | /// Sends a PUT request to an URL with text. |
151 | /// Sends a PUT request to an URL with text. |
|
153 | /// </summary> |
152 | /// </summary> |
|
154 | /// <param name="URL">the url to send the message to</param> |
153 | /// <param name="URL">the url to send the message to</param> |
|
155 | /// <param name="data">key-value pairs to send</param> |
154 | /// <param name="data">key-value pairs to send</param> |
|
156 | /// <param name="headers">headers to send with the request</param> |
155 | /// <param name="headers">headers to send with the request</param> |
|
157 | public async Task<byte[]> PUT(string URL, string data, Dictionary<string, string> headers) |
156 | public async Task<byte[]> PUT(string URL, string data, Dictionary<string, string> headers) |
|
158 | { |
157 | { |
|
159 | try |
158 | try |
|
160 | { |
159 | { |
|
161 | using (var content = |
160 | using (var content = |
|
162 | new StringContent(data, Encoding.UTF8, MediaType)) |
161 | new StringContent(data, Encoding.UTF8, MediaType)) |
|
163 | { |
162 | { |
|
164 | using ( |
163 | using ( |
|
165 | var request = new HttpRequestMessage |
164 | var request = new HttpRequestMessage |
|
166 | { |
165 | { |
|
167 | RequestUri = new Uri(URL), |
166 | RequestUri = new Uri(URL), |
|
168 | Method = HttpMethod.Put, |
167 | Method = HttpMethod.Put, |
|
169 | Content = content |
168 | Content = content |
|
170 | }) |
169 | }) |
|
171 | { |
170 | { |
|
172 | foreach (var header in headers) |
171 | foreach (var header in headers) |
|
173 | { |
172 | { |
|
174 | request.Headers.Add(header.Key, header.Value); |
173 | request.Headers.Add(header.Key, header.Value); |
|
175 | } |
174 | } |
|
176 | // Add some standard headers: |
175 | // Add some standard headers: |
|
177 | // Accept - for socially acceptable security of mod_sec |
176 | // Accept - for socially acceptable security of mod_sec |
|
178 | if (!headers.ContainsKey("Accept")) |
177 | if (!headers.ContainsKey("Accept")) |
|
179 | { |
178 | { |
|
180 | headers.Add("Accept", @"*/*"); |
179 | headers.Add("Accept", @"*/*"); |
|
181 | } |
180 | } |
|
182 | using (var response = await HTTPClient.SendAsync(request)) |
181 | using (var response = await HTTPClient.SendAsync(request)) |
|
183 | { |
182 | { |
|
184 | return response.IsSuccessStatusCode |
183 | return response.IsSuccessStatusCode |
|
185 | ? await response.Content.ReadAsByteArrayAsync() |
184 | ? await response.Content.ReadAsByteArrayAsync() |
|
186 | : null; |
185 | : null; |
|
187 | } |
186 | } |
|
188 | } |
187 | } |
|
189 | } |
188 | } |
|
190 | } |
189 | } |
|
191 | catch (Exception) |
190 | catch (Exception) |
|
192 | { |
191 | { |
|
193 | return null; |
192 | return null; |
|
194 | } |
193 | } |
|
195 | } |
194 | } |
|
196 | |
195 | |
|
197 | /////////////////////////////////////////////////////////////////////////// |
196 | /////////////////////////////////////////////////////////////////////////// |
|
198 | // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // |
197 | // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // |
|
199 | /////////////////////////////////////////////////////////////////////////// |
198 | /////////////////////////////////////////////////////////////////////////// |
|
200 | /// <summary> |
199 | /// <summary> |
|
201 | /// Sends a PUT request to an URL with binary data. |
200 | /// Sends a PUT request to an URL with binary data. |
|
202 | /// </summary> |
201 | /// </summary> |
|
203 | /// <param name="URL">the url to send the message to</param> |
202 | /// <param name="URL">the url to send the message to</param> |
|
204 | /// <param name="data">key-value pairs to send</param> |
203 | /// <param name="data">key-value pairs to send</param> |
|
205 | public async Task<byte[]> PUT(string URL, byte[] data) |
204 | public async Task<byte[]> PUT(string URL, byte[] data) |
|
206 | { |
205 | { |
|
207 | try |
206 | try |
|
208 | { |
207 | { |
|
209 | using (var content = new ByteArrayContent(data)) |
208 | using (var content = new ByteArrayContent(data)) |
|
210 | { |
209 | { |
|
211 | using (var response = await HTTPClient.PutAsync(URL, content)) |
210 | using (var response = await HTTPClient.PutAsync(URL, content)) |
|
212 | { |
211 | { |
|
213 | return response.IsSuccessStatusCode |
212 | return response.IsSuccessStatusCode |
|
214 | ? await response.Content.ReadAsByteArrayAsync() |
213 | ? await response.Content.ReadAsByteArrayAsync() |
|
215 | : null; |
214 | : null; |
|
216 | } |
215 | } |
|
217 | } |
216 | } |
|
218 | } |
217 | } |
|
219 | catch (Exception) |
218 | catch (Exception) |
|
220 | { |
219 | { |
|
221 | return null; |
220 | return null; |
|
222 | } |
221 | } |
|
223 | } |
222 | } |
|
224 | |
223 | |
|
225 | /////////////////////////////////////////////////////////////////////////// |
224 | /////////////////////////////////////////////////////////////////////////// |
|
226 | // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // |
225 | // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // |
|
227 | /////////////////////////////////////////////////////////////////////////// |
226 | /////////////////////////////////////////////////////////////////////////// |
|
228 | /// <summary> |
227 | /// <summary> |
|
229 | /// Sends a PUT request to an URL with stream content data. |
228 | /// Sends a PUT request to an URL with stream content data. |
|
230 | /// </summary> |
229 | /// </summary> |
|
231 | /// <param name="URL">the url to send the message to</param> |
230 | /// <param name="URL">the url to send the message to</param> |
|
232 | /// <param name="data">key-value pairs to send</param> |
231 | /// <param name="data">key-value pairs to send</param> |
|
233 | public async Task<byte[]> PUT(string URL, Stream data) |
232 | public async Task<byte[]> PUT(string URL, Stream data) |
|
234 | { |
233 | { |
|
235 | try |
234 | try |
|
236 | { |
235 | { |
|
237 | using (var streamContenet = new StreamContent(data)) |
236 | using (var streamContenet = new StreamContent(data)) |
|
238 | { |
237 | { |
|
239 | using (var response = await HTTPClient.PutAsync(URL, streamContenet)) |
238 | using (var response = await HTTPClient.PutAsync(URL, streamContenet)) |
|
240 | { |
239 | { |
|
241 | return response.IsSuccessStatusCode |
240 | return response.IsSuccessStatusCode |
|
242 | ? await response.Content.ReadAsByteArrayAsync() |
241 | ? await response.Content.ReadAsByteArrayAsync() |
|
243 | : null; |
242 | : null; |
|
244 | } |
243 | } |
|
245 | } |
244 | } |
|
246 | } |
245 | } |
|
247 | catch (Exception) |
246 | catch (Exception) |
|
248 | { |
247 | { |
|
249 | return null; |
248 | return null; |
|
250 | } |
249 | } |
|
251 | } |
250 | } |
|
252 | |
251 | |
|
253 | /////////////////////////////////////////////////////////////////////////// |
252 | /////////////////////////////////////////////////////////////////////////// |
|
254 | // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // |
253 | // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // |
|
255 | /////////////////////////////////////////////////////////////////////////// |
254 | /////////////////////////////////////////////////////////////////////////// |
|
256 | /// <summary> |
255 | /// <summary> |
|
257 | /// Sends a DELETE request to an URL with stream content data. |
256 | /// Sends a DELETE request to an URL with stream content data. |
|
258 | /// </summary> |
257 | /// </summary> |
|
259 | /// <param name="URL">the url to send the message to</param> |
258 | /// <param name="URL">the url to send the message to</param> |
|
260 | /// <param name="data">key-value pairs to send</param> |
259 | /// <param name="data">key-value pairs to send</param> |
|
261 | public async Task<byte[]> DELETE(string URL, Stream data) |
260 | public async Task<byte[]> DELETE(string URL, Stream data) |
|
262 | { |
261 | { |
|
263 | try |
262 | try |
|
264 | { |
263 | { |
|
265 | using (var streamContenet = new StreamContent(data)) |
264 | using (var streamContenet = new StreamContent(data)) |
|
266 | { |
265 | { |
|
267 | using (var request = new HttpRequestMessage(HttpMethod.Delete, URL) |
266 | using (var request = new HttpRequestMessage(HttpMethod.Delete, URL) |
|
268 | { |
267 | { |
|
269 | Content = streamContenet |
268 | Content = streamContenet |
|
270 | }) |
269 | }) |
|
271 | { |
270 | { |
|
272 | using (var response = await HTTPClient.SendAsync(request)) |
271 | using (var response = await HTTPClient.SendAsync(request)) |
|
273 | { |
272 | { |
|
274 | return response.IsSuccessStatusCode |
273 | return response.IsSuccessStatusCode |
|
275 | ? await response.Content.ReadAsByteArrayAsync() |
274 | ? await response.Content.ReadAsByteArrayAsync() |
|
276 | : null; |
275 | : null; |
|
277 | } |
276 | } |
|
278 | } |
277 | } |
|
279 | } |
278 | } |
|
280 | } |
279 | } |
|
281 | catch (Exception) |
280 | catch (Exception) |
|
282 | { |
281 | { |
|
283 | return null; |
282 | return null; |
|
284 | } |
283 | } |
|
285 | } |
284 | } |
|
286 | |
285 | |
|
287 | /////////////////////////////////////////////////////////////////////////// |
286 | /////////////////////////////////////////////////////////////////////////// |
|
288 | // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // |
287 | // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // |
|
289 | /////////////////////////////////////////////////////////////////////////// |
288 | /////////////////////////////////////////////////////////////////////////// |
|
290 | /// <summary> |
289 | /// <summary> |
|
291 | /// Sends a PUT request to an URL with text. |
290 | /// Sends a PUT request to an URL with text. |
|
292 | /// </summary> |
291 | /// </summary> |
|
293 | /// <param name="URL">the url to send the message to</param> |
292 | /// <param name="URL">the url to send the message to</param> |
|
294 | /// <param name="data">key-value pairs to send</param> |
293 | /// <param name="data">key-value pairs to send</param> |
|
295 | public async Task<byte[]> PUT(string URL, string data) |
294 | public async Task<byte[]> PUT(string URL, string data) |
|
296 | { |
295 | { |
|
297 | try |
296 | try |
|
298 | { |
297 | { |
|
299 | using (var content = |
298 | using (var content = |
|
300 | new StringContent(data, Encoding.UTF8, MediaType)) |
299 | new StringContent(data, Encoding.UTF8, MediaType)) |
|
301 | { |
300 | { |
|
302 | using (var response = await HTTPClient.PutAsync(URL, content)) |
301 | using (var response = await HTTPClient.PutAsync(URL, content)) |
|
303 | { |
302 | { |
|
304 | return response.IsSuccessStatusCode |
303 | return response.IsSuccessStatusCode |
|
305 | ? await response.Content.ReadAsByteArrayAsync() |
304 | ? await response.Content.ReadAsByteArrayAsync() |
|
306 | : null; |
305 | : null; |
|
307 | } |
306 | } |
|
308 | } |
307 | } |
|
309 | } |
308 | } |
|
310 | catch (Exception) |
309 | catch (Exception) |
|
311 | { |
310 | { |
|
312 | return null; |
311 | return null; |
|
313 | } |
312 | } |
|
314 | } |
313 | } |
|
315 | |
314 | |
|
316 | /////////////////////////////////////////////////////////////////////////// |
315 | /////////////////////////////////////////////////////////////////////////// |
|
317 | // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // |
316 | // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // |
|
318 | /////////////////////////////////////////////////////////////////////////// |
317 | /////////////////////////////////////////////////////////////////////////// |
|
319 | /// <summary> |
318 | /// <summary> |
|
320 | /// Sends a PUT request to an URL with text. |
319 | /// Sends a PUT request to an URL with text. |
|
321 | /// </summary> |
320 | /// </summary> |
|
322 | /// <param name="URL">the url to send the message to</param> |
321 | /// <param name="URL">the url to send the message to</param> |
|
323 | /// <param name="data">key-value pairs to send</param> |
322 | /// <param name="data">key-value pairs to send</param> |
|
324 | public async Task<byte[]> DELETE(string URL, string data) |
323 | public async Task<byte[]> DELETE(string URL, string data) |
|
325 | { |
324 | { |
|
326 | try |
325 | try |
|
327 | { |
326 | { |
|
328 | using (var content = |
327 | using (var content = |
|
329 | new StringContent(data, Encoding.UTF8, MediaType)) |
328 | new StringContent(data, Encoding.UTF8, MediaType)) |
|
330 | { |
329 | { |
|
331 | using (var request = new HttpRequestMessage(HttpMethod.Delete, URL) |
330 | using (var request = new HttpRequestMessage(HttpMethod.Delete, URL) |
|
332 | { |
331 | { |
|
333 | Content = content |
332 | Content = content |
|
334 | }) |
333 | }) |
|
335 | { |
334 | { |
|
336 | using (var response = await HTTPClient.SendAsync(request)) |
335 | using (var response = await HTTPClient.SendAsync(request)) |
|
337 | { |
336 | { |
|
338 | return response.IsSuccessStatusCode |
337 | return response.IsSuccessStatusCode |
|
339 | ? await response.Content.ReadAsByteArrayAsync() |
338 | ? await response.Content.ReadAsByteArrayAsync() |
|
340 | : null; |
339 | : null; |
|
341 | } |
340 | } |
|
342 | } |
341 | } |
|
343 | } |
342 | } |
|
344 | } |
343 | } |
|
345 | catch (Exception) |
344 | catch (Exception) |
|
346 | { |
345 | { |
|
347 | return null; |
346 | return null; |
|
348 | } |
347 | } |
|
349 | } |
348 | } |
|
350 | |
349 | |
|
351 | /////////////////////////////////////////////////////////////////////////// |
350 | /////////////////////////////////////////////////////////////////////////// |
|
352 | // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // |
351 | // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // |
|
353 | /////////////////////////////////////////////////////////////////////////// |
352 | /////////////////////////////////////////////////////////////////////////// |
|
354 | /// <summary> |
353 | /// <summary> |
|
355 | /// Sends a POST request to an URL with set key-value pairs. |
354 | /// Sends a POST request to an URL with set key-value pairs. |
|
356 | /// </summary> |
355 | /// </summary> |
|
357 | /// <param name="URL">the url to send the message to</param> |
356 | /// <param name="URL">the url to send the message to</param> |
|
358 | /// <param name="message">key-value pairs to send</param> |
357 | /// <param name="message">key-value pairs to send</param> |
|
359 | public async Task<byte[]> POST(string URL, Dictionary<string, string> message) |
358 | public async Task<byte[]> POST(string URL, Dictionary<string, string> message) |
|
360 | { |
359 | { |
|
361 | try |
360 | try |
|
362 | { |
361 | { |
|
363 | using (var content = |
362 | using (var content = |
|
364 | new StringContent(KeyValue.Encode(message), Encoding.UTF8, MediaType)) |
363 | new StringContent(KeyValue.Encode(message), Encoding.UTF8, MediaType)) |
|
365 | { |
364 | { |
|
366 | using (var response = await HTTPClient.PostAsync(URL, content)) |
365 | using (var response = await HTTPClient.PostAsync(URL, content)) |
|
367 | { |
366 | { |
|
368 | return response.IsSuccessStatusCode |
367 | return response.IsSuccessStatusCode |
|
369 | ? await response.Content.ReadAsByteArrayAsync() |
368 | ? await response.Content.ReadAsByteArrayAsync() |
|
370 | : null; |
369 | : null; |
|
371 | } |
370 | } |
|
372 | } |
371 | } |
|
373 | } |
372 | } |
|
374 | catch (Exception) |
373 | catch (Exception) |
|
375 | { |
374 | { |
|
376 | return null; |
375 | return null; |
|
377 | } |
376 | } |
|
378 | } |
377 | } |
|
379 | |
378 | |
|
380 | /////////////////////////////////////////////////////////////////////////// |
379 | /////////////////////////////////////////////////////////////////////////// |
|
381 | // Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 // |
380 | // Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 // |
|
382 | /////////////////////////////////////////////////////////////////////////// |
381 | /////////////////////////////////////////////////////////////////////////// |
|
383 | /// <summary> |
382 | /// <summary> |
|
384 | /// Sends a POST request to an URL with set message. |
383 | /// Sends a POST request to an URL with set message. |
|
385 | /// </summary> |
384 | /// </summary> |
|
386 | /// <param name="URL">the url to send the message to</param> |
385 | /// <param name="URL">the url to send the message to</param> |
|
387 | /// <param name="message">the message to send</param> |
386 | /// <param name="message">the message to send</param> |
|
388 | public async Task<byte[]> POST(string URL, string message) |
387 | public async Task<byte[]> POST(string URL, string message) |
|
389 | { |
388 | { |
|
390 | try |
389 | try |
|
391 | { |
390 | { |
|
392 | using (var content = |
391 | using (var content = |
|
393 | new StringContent(message, Encoding.UTF8, MediaType)) |
392 | new StringContent(message, Encoding.UTF8, MediaType)) |
|
394 | { |
393 | { |
|
395 | using (var response = await HTTPClient.PostAsync(URL, content)) |
394 | using (var response = await HTTPClient.PostAsync(URL, content)) |
|
396 | { |
395 | { |
|
397 | return response.IsSuccessStatusCode |
396 | return response.IsSuccessStatusCode |
|
398 | ? await response.Content.ReadAsByteArrayAsync() |
397 | ? await response.Content.ReadAsByteArrayAsync() |
|
399 | : null; |
398 | : null; |
|
400 | } |
399 | } |
|
401 | } |
400 | } |
|
402 | } |
401 | } |
|
403 | catch (Exception) |
402 | catch (Exception) |
|
404 | { |
403 | { |
|
405 | return null; |
404 | return null; |
|
406 | } |
405 | } |
|
407 | } |
406 | } |
|
408 | |
407 | |
|
409 | /////////////////////////////////////////////////////////////////////////// |
408 | /////////////////////////////////////////////////////////////////////////// |
|
410 | // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // |
409 | // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // |
|
411 | /////////////////////////////////////////////////////////////////////////// |
410 | /////////////////////////////////////////////////////////////////////////// |
|
412 | /// <summary> |
411 | /// <summary> |
|
413 | /// Sends a POST request to an URL with set key-value pairs. |
412 | /// Sends a POST request to an URL with set key-value pairs. |
|
414 | /// </summary> |
413 | /// </summary> |
|
415 | /// <param name="URL">the url to send the message to</param> |
414 | /// <param name="URL">the url to send the message to</param> |
|
416 | /// <param name="message">key-value pairs to send</param> |
415 | /// <param name="message">key-value pairs to send</param> |
|
417 | public async Task<byte[]> POST(string URL, IEnumerable<KeyValuePair<string, string>> message) |
416 | public async Task<byte[]> POST(string URL, IEnumerable<KeyValuePair<string, string>> message) |
|
418 | { |
417 | { |
|
419 | try |
418 | try |
|
420 | { |
419 | { |
|
421 | using (var content = |
420 | using (var content = |
|
422 | new StringContent(KeyValue.Encode(message), Encoding.UTF8, MediaType)) |
421 | new StringContent(KeyValue.Encode(message), Encoding.UTF8, MediaType)) |
|
423 | { |
422 | { |
|
424 | using (var response = await HTTPClient.PostAsync(URL, content)) |
423 | using (var response = await HTTPClient.PostAsync(URL, content)) |
|
425 | { |
424 | { |
|
426 | return response.IsSuccessStatusCode |
425 | return response.IsSuccessStatusCode |
|
427 | ? await response.Content.ReadAsByteArrayAsync() |
426 | ? await response.Content.ReadAsByteArrayAsync() |
|
428 | : null; |
427 | : null; |
|
429 | } |
428 | } |
|
430 | } |
429 | } |
|
431 | } |
430 | } |
|
432 | catch (Exception) |
431 | catch (Exception) |
|
433 | { |
432 | { |
|
434 | return null; |
433 | return null; |
|
435 | } |
434 | } |
|
436 | } |
435 | } |
|
437 | |
436 | |
|
438 | /////////////////////////////////////////////////////////////////////////// |
437 | /////////////////////////////////////////////////////////////////////////// |
|
439 | // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // |
438 | // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // |
|
440 | /////////////////////////////////////////////////////////////////////////// |
439 | /////////////////////////////////////////////////////////////////////////// |
|
441 | /// <summary> |
440 | /// <summary> |
|
442 | /// Sends a GET request to an URL with set key-value pairs. |
441 | /// Sends a GET request to an URL with set key-value pairs. |
|
443 | /// </summary> |
442 | /// </summary> |
|
444 | /// <param name="URL">the url to send the message to</param> |
443 | /// <param name="URL">the url to send the message to</param> |
|
445 | /// <param name="message">key-value pairs to send</param> |
444 | /// <param name="message">key-value pairs to send</param> |
|
446 | public async Task<byte[]> GET(string URL, Dictionary<string, string> message) |
445 | public async Task<byte[]> GET(string URL, Dictionary<string, string> message) |
|
447 | { |
446 | { |
|
448 | try |
447 | try |
|
449 | { |
448 | { |
|
450 | using (var response = |
449 | using (var response = |
|
451 | await HTTPClient.GetAsync($"{URL}?{KeyValue.Encode(message)}")) |
450 | await HTTPClient.GetAsync($"{URL}?{KeyValue.Encode(message)}")) |
|
452 | { |
451 | { |
|
453 | return response.IsSuccessStatusCode ? |
452 | return response.IsSuccessStatusCode ? |
|
454 | await response.Content.ReadAsByteArrayAsync() : null; |
453 | await response.Content.ReadAsByteArrayAsync() : null; |
|
455 | } |
454 | } |
|
456 | } |
455 | } |
|
457 | catch (Exception) |
456 | catch (Exception) |
|
458 | { |
457 | { |
|
459 | return null; |
458 | return null; |
|
460 | } |
459 | } |
|
461 | } |
460 | } |
|
462 | |
461 | |
|
463 | /////////////////////////////////////////////////////////////////////////// |
462 | /////////////////////////////////////////////////////////////////////////// |
|
464 | // Copyright (C) 2016 Wizardry and Steamworks - License: GNU GPLv3 // |
463 | // Copyright (C) 2016 Wizardry and Steamworks - License: GNU GPLv3 // |
|
465 | /////////////////////////////////////////////////////////////////////////// |
464 | /////////////////////////////////////////////////////////////////////////// |
|
466 | /// <summary> |
465 | /// <summary> |
|
467 | /// Sends a GET request to an URL with set key-value pairs. |
466 | /// Sends a GET request to an URL with set key-value pairs. |
|
468 | /// </summary> |
467 | /// </summary> |
|
469 | /// <param name="URL">the url to send the message to</param> |
468 | /// <param name="URL">the url to send the message to</param> |
|
470 | public async Task<byte[]> GET(string URL) |
469 | public async Task<byte[]> GET(string URL) |
|
471 | { |
470 | { |
|
472 | try |
471 | try |
|
473 | { |
472 | { |
|
474 | using (var response = await HTTPClient.GetAsync(URL)) |
473 | using (var response = await HTTPClient.GetAsync(URL)) |
|
475 | { |
474 | { |
|
476 | return response.IsSuccessStatusCode ? |
475 | return response.IsSuccessStatusCode ? |
|
477 | await response.Content.ReadAsByteArrayAsync() : null; |
476 | await response.Content.ReadAsByteArrayAsync() : null; |
|
478 | } |
477 | } |
|
479 | } |
478 | } |
|
480 | catch (Exception) |
479 | catch (Exception) |
|
481 | { |
480 | { |
|
482 | return null; |
481 | return null; |
|
483 | } |
482 | } |
|
484 | } |
483 | } |
|
- | 484 | |
||
- | 485 | public void Dispose() |
||
- | 486 | { |
||
- | 487 | ((IDisposable)HTTPClient).Dispose(); |
||
- | 488 | } |
||
485 | } |
489 | } |
|
486 | } |
490 | } |
|
487 | |
491 | |