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