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