wasSharp – Blame information for rev 18

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 switch (headers != null)
10 office 69 {
17 office 70 case false:
18 office 71 headers = new Dictionary<string, string> {{"Accept", @"*/*"}};
17 office 72 break;
73 default:
74 if (!headers.ContainsKey("Accept"))
75 {
76 headers.Add("Accept", @"*/*");
77 }
78 break;
10 office 79 }
17 office 80 foreach (var header in headers)
81 {
82 HTTPClient.DefaultRequestHeaders.Add(header.Key, header.Value);
83 }
10 office 84 HTTPClient.Timeout = TimeSpan.FromMilliseconds(timeout);
85 MediaType = mediaType;
86 }
87  
88 ///////////////////////////////////////////////////////////////////////////
89 // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 //
90 ///////////////////////////////////////////////////////////////////////////
91 /// <summary>
92 /// Sends a PUT request to an URL with binary data.
93 /// </summary>
94 /// <param name="URL">the url to send the message to</param>
95 /// <param name="data">key-value pairs to send</param>
96 /// <param name="headers">headers to send with the request</param>
97 public async Task<byte[]> PUT(string URL, byte[] data, Dictionary<string, string> headers)
98 {
99 try
100 {
101 using (var content = new ByteArrayContent(data))
102 {
103 using (
104 var request = new HttpRequestMessage
105 {
106 RequestUri = new Uri(URL),
107 Method = HttpMethod.Put,
108 Content = content
109 })
110 {
111 foreach (var header in headers)
112 {
113 request.Headers.Add(header.Key, header.Value);
114 }
17 office 115 // Add some standard headers:
116 // Accept - for socially acceptable security of mod_sec
117 if (!headers.ContainsKey("Accept"))
118 {
119 headers.Add("Accept", @"*/*");
120 }
10 office 121 using (var response = await HTTPClient.SendAsync(request))
122 {
123 return response.IsSuccessStatusCode
124 ? await response.Content.ReadAsByteArrayAsync()
125 : null;
126 }
127 }
128 }
129 }
130 catch (Exception)
131 {
132 return null;
133 }
134 }
135  
136 ///////////////////////////////////////////////////////////////////////////
137 // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 //
138 ///////////////////////////////////////////////////////////////////////////
139 /// <summary>
140 /// Sends a PUT request to an URL with text.
141 /// </summary>
142 /// <param name="URL">the url to send the message to</param>
143 /// <param name="data">key-value pairs to send</param>
144 /// <param name="headers">headers to send with the request</param>
145 public async Task<byte[]> PUT(string URL, string data, Dictionary<string, string> headers)
146 {
147 try
148 {
149 using (var content =
150 new StringContent(data, Encoding.UTF8, MediaType))
151 {
152 using (
153 var request = new HttpRequestMessage
154 {
155 RequestUri = new Uri(URL),
156 Method = HttpMethod.Put,
157 Content = content
158 })
159 {
160 foreach (var header in headers)
161 {
162 request.Headers.Add(header.Key, header.Value);
163 }
17 office 164 // Add some standard headers:
165 // Accept - for socially acceptable security of mod_sec
166 if (!headers.ContainsKey("Accept"))
167 {
168 headers.Add("Accept", @"*/*");
169 }
10 office 170 using (var response = await HTTPClient.SendAsync(request))
171 {
172 return response.IsSuccessStatusCode
173 ? await response.Content.ReadAsByteArrayAsync()
174 : null;
175 }
176 }
177 }
178 }
179 catch (Exception)
180 {
181 return null;
182 }
183 }
184  
185 ///////////////////////////////////////////////////////////////////////////
186 // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 //
187 ///////////////////////////////////////////////////////////////////////////
188 /// <summary>
189 /// Sends a PUT request to an URL with binary data.
190 /// </summary>
191 /// <param name="URL">the url to send the message to</param>
192 /// <param name="data">key-value pairs to send</param>
193 public async Task<byte[]> PUT(string URL, byte[] data)
194 {
195 try
196 {
197 using (var content = new ByteArrayContent(data))
198 {
199 using (var response = await HTTPClient.PutAsync(URL, content))
200 {
201 return response.IsSuccessStatusCode
202 ? await response.Content.ReadAsByteArrayAsync()
203 : null;
204 }
205 }
206 }
207 catch (Exception)
208 {
209 return null;
210 }
211 }
212  
213 ///////////////////////////////////////////////////////////////////////////
214 // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 //
215 ///////////////////////////////////////////////////////////////////////////
216 /// <summary>
217 /// Sends a PUT request to an URL with text.
218 /// </summary>
219 /// <param name="URL">the url to send the message to</param>
220 /// <param name="data">key-value pairs to send</param>
221 public async Task<byte[]> PUT(string URL, string data)
222 {
223 try
224 {
225 using (var content =
226 new StringContent(data, Encoding.UTF8, MediaType))
227 {
228 using (var response = await HTTPClient.PutAsync(URL, content))
229 {
230 return response.IsSuccessStatusCode
231 ? await response.Content.ReadAsByteArrayAsync()
232 : null;
233 }
234 }
235 }
236 catch (Exception)
237 {
238 return null;
239 }
240 }
241  
242 ///////////////////////////////////////////////////////////////////////////
243 // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 //
244 ///////////////////////////////////////////////////////////////////////////
245 /// <summary>
246 /// Sends a POST request to an URL with set key-value pairs.
247 /// </summary>
248 /// <param name="URL">the url to send the message to</param>
249 /// <param name="message">key-value pairs to send</param>
250 public async Task<byte[]> POST(string URL, Dictionary<string, string> message)
251 {
252 try
253 {
254 using (var content =
255 new StringContent(KeyValue.Encode(message), Encoding.UTF8, MediaType))
256 {
257 using (var response = await HTTPClient.PostAsync(URL, content))
258 {
259 return response.IsSuccessStatusCode
260 ? await response.Content.ReadAsByteArrayAsync()
261 : null;
262 }
263 }
264 }
265 catch (Exception)
266 {
267 return null;
268 }
269 }
270  
271 ///////////////////////////////////////////////////////////////////////////
272 // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 //
273 ///////////////////////////////////////////////////////////////////////////
274 /// <summary>
275 /// Sends a GET request to an URL with set key-value pairs.
276 /// </summary>
277 /// <param name="URL">the url to send the message to</param>
278 /// <param name="message">key-value pairs to send</param>
279 public async Task<byte[]> GET(string URL, Dictionary<string, string> message)
280 {
281 try
282 {
283 using (var response =
284 await HTTPClient.GetAsync(URL + "?" + KeyValue.Encode(message)))
285 {
286 return response.IsSuccessStatusCode ? await response.Content.ReadAsByteArrayAsync() : null;
287 }
288 }
289 catch (Exception)
290 {
291 return null;
292 }
293 }
14 office 294  
295 ///////////////////////////////////////////////////////////////////////////
296 // Copyright (C) 2016 Wizardry and Steamworks - License: GNU GPLv3 //
297 ///////////////////////////////////////////////////////////////////////////
298 /// <summary>
299 /// Sends a GET request to an URL with set key-value pairs.
300 /// </summary>
301 /// <param name="URL">the url to send the message to</param>
302 public async Task<byte[]> GET(string URL)
303 {
304 try
305 {
306 using (var response = await HTTPClient.GetAsync(URL))
307 {
308 return response.IsSuccessStatusCode ? await response.Content.ReadAsByteArrayAsync() : null;
309 }
310 }
311 catch (Exception)
312 {
313 return null;
314 }
315 }
316  
317 public void Dispose()
318 {
17 office 319 HTTPClient?.Dispose();
14 office 320 }
10 office 321 }
322 }