wasSharp – Blame information for rev 17

Subversion Repositories:
Rev:
Rev Author Line No. Line
10 office 1 ///////////////////////////////////////////////////////////////////////////
2 // Copyright (C) Wizardry and Steamworks 2013 - License: GNU GPLv3 //
3 // Please see: http://www.gnu.org/licenses/gpl.html for legal details, //
4 // rights of fair usage, the disclaimer and warranty conditions. //
5 ///////////////////////////////////////////////////////////////////////////
6  
7 using System;
8 using System.Collections.Generic;
9 using System.Net;
10 using System.Net.Http;
11 using System.Net.Http.Headers;
12 using System.Text;
13 using System.Threading.Tasks;
17 office 14 using wasSharp.Collections.Utilities;
10 office 15  
16 namespace wasSharp.Web
17 {
18 ///////////////////////////////////////////////////////////////////////////
19 // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 //
20 ///////////////////////////////////////////////////////////////////////////
21 // <summary>A portable HTTP client.</summar>
14 office 22 public class wasHTTPClient : IDisposable
10 office 23 {
24 private readonly HttpClient HTTPClient;
25 private readonly string MediaType;
26  
27 public wasHTTPClient(ProductInfoHeaderValue userAgent, CookieContainer cookieContainer, string mediaType,
28 uint timeout) : this(userAgent, cookieContainer, mediaType, null, null, timeout)
29 {
30 }
31  
32 public wasHTTPClient(ProductInfoHeaderValue userAgent, string mediaType, uint timeout)
33 : this(userAgent, new CookieContainer(), mediaType, null, null, timeout)
34 {
35 }
36  
37 public wasHTTPClient(ProductInfoHeaderValue userAgent, CookieContainer cookieContainer, string mediaType,
38 AuthenticationHeaderValue authentication, Dictionary<string, string> headers, uint timeout)
39 {
40 var HTTPClientHandler = new HttpClientHandler
41 {
42 CookieContainer = cookieContainer,
43 UseCookies = true
44 };
45 if (HTTPClientHandler.SupportsRedirectConfiguration)
46 {
47 HTTPClientHandler.AllowAutoRedirect = true;
48 }
49 if (HTTPClientHandler.SupportsProxy)
50 {
51 HTTPClientHandler.Proxy = WebRequest.DefaultWebProxy;
52 HTTPClientHandler.UseProxy = true;
53 }
54 if (HTTPClientHandler.SupportsAutomaticDecompression)
55 {
56 HTTPClientHandler.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
57 }
58 HTTPClientHandler.ClientCertificateOptions = ClientCertificateOption.Automatic;
59  
60 HTTPClient = new HttpClient(HTTPClientHandler, false);
61 HTTPClient.DefaultRequestHeaders.UserAgent.Add(userAgent);
62 if (authentication != null)
63 {
64 HTTPClient.DefaultRequestHeaders.Authorization = authentication;
65 }
17 office 66 // Add some standard headers:
67 // Accept - for socially acceptable security of mod_sec
68 // Accept-Encoding - since we want to use compression if possible
69 switch (headers != null)
10 office 70 {
17 office 71 case false:
72 headers = new Dictionary<string, string> {{"Accept", @"*/*"}, {"Accept-Encoding", "gzip,defalate"}};
73 break;
74 default:
75 if (!headers.ContainsKey("Accept"))
76 {
77 headers.Add("Accept", @"*/*");
78 }
79 if (!headers.ContainsKey("Accept-Encoding"))
80 {
81 headers.Add("Accept-Encoding", "gzip,deflate");
82 }
83 break;
10 office 84 }
17 office 85 foreach (var header in headers)
86 {
87 HTTPClient.DefaultRequestHeaders.Add(header.Key, header.Value);
88 }
10 office 89 HTTPClient.Timeout = TimeSpan.FromMilliseconds(timeout);
90 MediaType = mediaType;
91 }
92  
93 ///////////////////////////////////////////////////////////////////////////
94 // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 //
95 ///////////////////////////////////////////////////////////////////////////
96 /// <summary>
97 /// Sends a PUT request to an URL with binary data.
98 /// </summary>
99 /// <param name="URL">the url to send the message to</param>
100 /// <param name="data">key-value pairs to send</param>
101 /// <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 {
104 try
105 {
106 using (var content = new ByteArrayContent(data))
107 {
108 using (
109 var request = new HttpRequestMessage
110 {
111 RequestUri = new Uri(URL),
112 Method = HttpMethod.Put,
113 Content = content
114 })
115 {
116 foreach (var header in headers)
117 {
118 request.Headers.Add(header.Key, header.Value);
119 }
17 office 120 // Add some standard headers:
121 // Accept - for socially acceptable security of mod_sec
122 // Accept-Encoding - since we want to use compression if possible
123 if (!headers.ContainsKey("Accept"))
124 {
125 headers.Add("Accept", @"*/*");
126 }
127 if (!headers.ContainsKey("Accept-Encoding"))
128 {
129 headers.Add("Accept-Encoding", "gzip,deflate");
130 }
10 office 131 using (var response = await HTTPClient.SendAsync(request))
132 {
133 return response.IsSuccessStatusCode
134 ? await response.Content.ReadAsByteArrayAsync()
135 : null;
136 }
137 }
138 }
139 }
140 catch (Exception)
141 {
142 return null;
143 }
144 }
145  
146 ///////////////////////////////////////////////////////////////////////////
147 // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 //
148 ///////////////////////////////////////////////////////////////////////////
149 /// <summary>
150 /// Sends a PUT request to an URL with text.
151 /// </summary>
152 /// <param name="URL">the url to send the message to</param>
153 /// <param name="data">key-value pairs to send</param>
154 /// <param name="headers">headers to send with the request</param>
155 public async Task<byte[]> PUT(string URL, string data, Dictionary<string, string> headers)
156 {
157 try
158 {
159 using (var content =
160 new StringContent(data, Encoding.UTF8, MediaType))
161 {
162 using (
163 var request = new HttpRequestMessage
164 {
165 RequestUri = new Uri(URL),
166 Method = HttpMethod.Put,
167 Content = content
168 })
169 {
170 foreach (var header in headers)
171 {
172 request.Headers.Add(header.Key, header.Value);
173 }
17 office 174 // Add some standard headers:
175 // Accept - for socially acceptable security of mod_sec
176 // Accept-Encoding - since we want to use compression if possible
177 if (!headers.ContainsKey("Accept"))
178 {
179 headers.Add("Accept", @"*/*");
180 }
181 if (!headers.ContainsKey("Accept-Encoding"))
182 {
183 headers.Add("Accept-Encoding", "gzip,deflate");
184 }
10 office 185 using (var response = await HTTPClient.SendAsync(request))
186 {
187 return response.IsSuccessStatusCode
188 ? await response.Content.ReadAsByteArrayAsync()
189 : null;
190 }
191 }
192 }
193 }
194 catch (Exception)
195 {
196 return null;
197 }
198 }
199  
200 ///////////////////////////////////////////////////////////////////////////
201 // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 //
202 ///////////////////////////////////////////////////////////////////////////
203 /// <summary>
204 /// Sends a PUT request to an URL with binary data.
205 /// </summary>
206 /// <param name="URL">the url to send the message to</param>
207 /// <param name="data">key-value pairs to send</param>
208 public async Task<byte[]> PUT(string URL, byte[] data)
209 {
210 try
211 {
212 using (var content = new ByteArrayContent(data))
213 {
214 using (var response = await HTTPClient.PutAsync(URL, content))
215 {
216 return response.IsSuccessStatusCode
217 ? await response.Content.ReadAsByteArrayAsync()
218 : null;
219 }
220 }
221 }
222 catch (Exception)
223 {
224 return null;
225 }
226 }
227  
228 ///////////////////////////////////////////////////////////////////////////
229 // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 //
230 ///////////////////////////////////////////////////////////////////////////
231 /// <summary>
232 /// Sends a PUT request to an URL with text.
233 /// </summary>
234 /// <param name="URL">the url to send the message to</param>
235 /// <param name="data">key-value pairs to send</param>
236 public async Task<byte[]> PUT(string URL, string data)
237 {
238 try
239 {
240 using (var content =
241 new StringContent(data, Encoding.UTF8, MediaType))
242 {
243 using (var response = await HTTPClient.PutAsync(URL, content))
244 {
245 return response.IsSuccessStatusCode
246 ? await response.Content.ReadAsByteArrayAsync()
247 : null;
248 }
249 }
250 }
251 catch (Exception)
252 {
253 return null;
254 }
255 }
256  
257 ///////////////////////////////////////////////////////////////////////////
258 // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 //
259 ///////////////////////////////////////////////////////////////////////////
260 /// <summary>
261 /// Sends a POST request to an URL with set key-value pairs.
262 /// </summary>
263 /// <param name="URL">the url to send the message to</param>
264 /// <param name="message">key-value pairs to send</param>
265 public async Task<byte[]> POST(string URL, Dictionary<string, string> message)
266 {
267 try
268 {
269 using (var content =
270 new StringContent(KeyValue.Encode(message), Encoding.UTF8, MediaType))
271 {
272 using (var response = await HTTPClient.PostAsync(URL, content))
273 {
274 return response.IsSuccessStatusCode
275 ? await response.Content.ReadAsByteArrayAsync()
276 : null;
277 }
278 }
279 }
280 catch (Exception)
281 {
282 return null;
283 }
284 }
285  
286 ///////////////////////////////////////////////////////////////////////////
287 // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 //
288 ///////////////////////////////////////////////////////////////////////////
289 /// <summary>
290 /// Sends a GET request to an URL with set key-value pairs.
291 /// </summary>
292 /// <param name="URL">the url to send the message to</param>
293 /// <param name="message">key-value pairs to send</param>
294 public async Task<byte[]> GET(string URL, Dictionary<string, string> message)
295 {
296 try
297 {
298 using (var response =
299 await HTTPClient.GetAsync(URL + "?" + KeyValue.Encode(message)))
300 {
301 return response.IsSuccessStatusCode ? await response.Content.ReadAsByteArrayAsync() : null;
302 }
303 }
304 catch (Exception)
305 {
306 return null;
307 }
308 }
14 office 309  
310 ///////////////////////////////////////////////////////////////////////////
311 // Copyright (C) 2016 Wizardry and Steamworks - License: GNU GPLv3 //
312 ///////////////////////////////////////////////////////////////////////////
313 /// <summary>
314 /// Sends a GET request to an URL with set key-value pairs.
315 /// </summary>
316 /// <param name="URL">the url to send the message to</param>
317 public async Task<byte[]> GET(string URL)
318 {
319 try
320 {
321 using (var response = await HTTPClient.GetAsync(URL))
322 {
323 return response.IsSuccessStatusCode ? await response.Content.ReadAsByteArrayAsync() : null;
324 }
325 }
326 catch (Exception)
327 {
328 return null;
329 }
330 }
331  
332 public void Dispose()
333 {
17 office 334 HTTPClient?.Dispose();
14 office 335 }
10 office 336 }
337 }