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