wasSharp – Blame information for rev 14
?pathlinks?
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; |
||
14 | |||
15 | namespace wasSharp.Web |
||
16 | { |
||
17 | /////////////////////////////////////////////////////////////////////////// |
||
18 | // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // |
||
19 | /////////////////////////////////////////////////////////////////////////// |
||
20 | // <summary>A portable HTTP client.</summar> |
||
14 | office | 21 | public class wasHTTPClient : IDisposable |
10 | office | 22 | { |
23 | private readonly HttpClient HTTPClient; |
||
24 | private readonly string MediaType; |
||
25 | |||
26 | public wasHTTPClient(ProductInfoHeaderValue userAgent, CookieContainer cookieContainer, string mediaType, |
||
27 | uint timeout) : this(userAgent, cookieContainer, mediaType, null, null, timeout) |
||
28 | { |
||
29 | } |
||
30 | |||
31 | public wasHTTPClient(ProductInfoHeaderValue userAgent, string mediaType, uint timeout) |
||
32 | : this(userAgent, new CookieContainer(), mediaType, null, null, timeout) |
||
33 | { |
||
34 | } |
||
35 | |||
36 | public wasHTTPClient(ProductInfoHeaderValue userAgent, CookieContainer cookieContainer, string mediaType, |
||
37 | AuthenticationHeaderValue authentication, Dictionary<string, string> headers, uint timeout) |
||
38 | { |
||
39 | var HTTPClientHandler = new HttpClientHandler |
||
40 | { |
||
41 | CookieContainer = cookieContainer, |
||
42 | UseCookies = true |
||
43 | }; |
||
44 | if (HTTPClientHandler.SupportsRedirectConfiguration) |
||
45 | { |
||
46 | HTTPClientHandler.AllowAutoRedirect = true; |
||
47 | } |
||
48 | if (HTTPClientHandler.SupportsProxy) |
||
49 | { |
||
50 | HTTPClientHandler.Proxy = WebRequest.DefaultWebProxy; |
||
51 | HTTPClientHandler.UseProxy = true; |
||
52 | } |
||
53 | if (HTTPClientHandler.SupportsAutomaticDecompression) |
||
54 | { |
||
55 | HTTPClientHandler.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate; |
||
56 | } |
||
57 | HTTPClientHandler.ClientCertificateOptions = ClientCertificateOption.Automatic; |
||
58 | |||
59 | HTTPClient = new HttpClient(HTTPClientHandler, false); |
||
60 | HTTPClient.DefaultRequestHeaders.UserAgent.Add(userAgent); |
||
61 | if (authentication != null) |
||
62 | { |
||
63 | HTTPClient.DefaultRequestHeaders.Authorization = authentication; |
||
64 | } |
||
65 | if (headers != null) |
||
66 | { |
||
67 | foreach (var header in headers) |
||
68 | { |
||
69 | HTTPClient.DefaultRequestHeaders.Add(header.Key, header.Value); |
||
70 | } |
||
71 | } |
||
72 | HTTPClient.Timeout = TimeSpan.FromMilliseconds(timeout); |
||
73 | MediaType = mediaType; |
||
74 | } |
||
75 | |||
76 | /////////////////////////////////////////////////////////////////////////// |
||
77 | // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // |
||
78 | /////////////////////////////////////////////////////////////////////////// |
||
79 | /// <summary> |
||
80 | /// Sends a PUT request to an URL with binary data. |
||
81 | /// </summary> |
||
82 | /// <param name="URL">the url to send the message to</param> |
||
83 | /// <param name="data">key-value pairs to send</param> |
||
84 | /// <param name="headers">headers to send with the request</param> |
||
85 | public async Task<byte[]> PUT(string URL, byte[] data, Dictionary<string, string> headers) |
||
86 | { |
||
87 | try |
||
88 | { |
||
89 | using (var content = new ByteArrayContent(data)) |
||
90 | { |
||
91 | using ( |
||
92 | var request = new HttpRequestMessage |
||
93 | { |
||
94 | RequestUri = new Uri(URL), |
||
95 | Method = HttpMethod.Put, |
||
96 | Content = content |
||
97 | }) |
||
98 | { |
||
99 | foreach (var header in headers) |
||
100 | { |
||
101 | request.Headers.Add(header.Key, header.Value); |
||
102 | } |
||
103 | using (var response = await HTTPClient.SendAsync(request)) |
||
104 | { |
||
105 | return response.IsSuccessStatusCode |
||
106 | ? await response.Content.ReadAsByteArrayAsync() |
||
107 | : null; |
||
108 | } |
||
109 | } |
||
110 | } |
||
111 | } |
||
112 | catch (Exception) |
||
113 | { |
||
114 | return null; |
||
115 | } |
||
116 | } |
||
117 | |||
118 | /////////////////////////////////////////////////////////////////////////// |
||
119 | // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // |
||
120 | /////////////////////////////////////////////////////////////////////////// |
||
121 | /// <summary> |
||
122 | /// Sends a PUT request to an URL with text. |
||
123 | /// </summary> |
||
124 | /// <param name="URL">the url to send the message to</param> |
||
125 | /// <param name="data">key-value pairs to send</param> |
||
126 | /// <param name="headers">headers to send with the request</param> |
||
127 | public async Task<byte[]> PUT(string URL, string data, Dictionary<string, string> headers) |
||
128 | { |
||
129 | try |
||
130 | { |
||
131 | using (var content = |
||
132 | new StringContent(data, Encoding.UTF8, MediaType)) |
||
133 | { |
||
134 | using ( |
||
135 | var request = new HttpRequestMessage |
||
136 | { |
||
137 | RequestUri = new Uri(URL), |
||
138 | Method = HttpMethod.Put, |
||
139 | Content = content |
||
140 | }) |
||
141 | { |
||
142 | foreach (var header in headers) |
||
143 | { |
||
144 | request.Headers.Add(header.Key, header.Value); |
||
145 | } |
||
146 | using (var response = await HTTPClient.SendAsync(request)) |
||
147 | { |
||
148 | return response.IsSuccessStatusCode |
||
149 | ? await response.Content.ReadAsByteArrayAsync() |
||
150 | : null; |
||
151 | } |
||
152 | } |
||
153 | } |
||
154 | } |
||
155 | catch (Exception) |
||
156 | { |
||
157 | return null; |
||
158 | } |
||
159 | } |
||
160 | |||
161 | /////////////////////////////////////////////////////////////////////////// |
||
162 | // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // |
||
163 | /////////////////////////////////////////////////////////////////////////// |
||
164 | /// <summary> |
||
165 | /// Sends a PUT request to an URL with binary data. |
||
166 | /// </summary> |
||
167 | /// <param name="URL">the url to send the message to</param> |
||
168 | /// <param name="data">key-value pairs to send</param> |
||
169 | public async Task<byte[]> PUT(string URL, byte[] data) |
||
170 | { |
||
171 | try |
||
172 | { |
||
173 | using (var content = new ByteArrayContent(data)) |
||
174 | { |
||
175 | using (var response = await HTTPClient.PutAsync(URL, content)) |
||
176 | { |
||
177 | return response.IsSuccessStatusCode |
||
178 | ? await response.Content.ReadAsByteArrayAsync() |
||
179 | : null; |
||
180 | } |
||
181 | } |
||
182 | } |
||
183 | catch (Exception) |
||
184 | { |
||
185 | return null; |
||
186 | } |
||
187 | } |
||
188 | |||
189 | /////////////////////////////////////////////////////////////////////////// |
||
190 | // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // |
||
191 | /////////////////////////////////////////////////////////////////////////// |
||
192 | /// <summary> |
||
193 | /// Sends a PUT request to an URL with text. |
||
194 | /// </summary> |
||
195 | /// <param name="URL">the url to send the message to</param> |
||
196 | /// <param name="data">key-value pairs to send</param> |
||
197 | public async Task<byte[]> PUT(string URL, string data) |
||
198 | { |
||
199 | try |
||
200 | { |
||
201 | using (var content = |
||
202 | new StringContent(data, Encoding.UTF8, MediaType)) |
||
203 | { |
||
204 | using (var response = await HTTPClient.PutAsync(URL, content)) |
||
205 | { |
||
206 | return response.IsSuccessStatusCode |
||
207 | ? await response.Content.ReadAsByteArrayAsync() |
||
208 | : null; |
||
209 | } |
||
210 | } |
||
211 | } |
||
212 | catch (Exception) |
||
213 | { |
||
214 | return null; |
||
215 | } |
||
216 | } |
||
217 | |||
218 | /////////////////////////////////////////////////////////////////////////// |
||
219 | // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // |
||
220 | /////////////////////////////////////////////////////////////////////////// |
||
221 | /// <summary> |
||
222 | /// Sends a POST request to an URL with set key-value pairs. |
||
223 | /// </summary> |
||
224 | /// <param name="URL">the url to send the message to</param> |
||
225 | /// <param name="message">key-value pairs to send</param> |
||
226 | public async Task<byte[]> POST(string URL, Dictionary<string, string> message) |
||
227 | { |
||
228 | try |
||
229 | { |
||
230 | using (var content = |
||
231 | new StringContent(KeyValue.Encode(message), Encoding.UTF8, MediaType)) |
||
232 | { |
||
233 | using (var response = await HTTPClient.PostAsync(URL, content)) |
||
234 | { |
||
235 | return response.IsSuccessStatusCode |
||
236 | ? await response.Content.ReadAsByteArrayAsync() |
||
237 | : null; |
||
238 | } |
||
239 | } |
||
240 | } |
||
241 | catch (Exception) |
||
242 | { |
||
243 | return null; |
||
244 | } |
||
245 | } |
||
246 | |||
247 | /////////////////////////////////////////////////////////////////////////// |
||
248 | // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // |
||
249 | /////////////////////////////////////////////////////////////////////////// |
||
250 | /// <summary> |
||
251 | /// Sends a GET request to an URL with set key-value pairs. |
||
252 | /// </summary> |
||
253 | /// <param name="URL">the url to send the message to</param> |
||
254 | /// <param name="message">key-value pairs to send</param> |
||
255 | public async Task<byte[]> GET(string URL, Dictionary<string, string> message) |
||
256 | { |
||
257 | try |
||
258 | { |
||
259 | using (var response = |
||
260 | await HTTPClient.GetAsync(URL + "?" + KeyValue.Encode(message))) |
||
261 | { |
||
262 | return response.IsSuccessStatusCode ? await response.Content.ReadAsByteArrayAsync() : null; |
||
263 | } |
||
264 | } |
||
265 | catch (Exception) |
||
266 | { |
||
267 | return null; |
||
268 | } |
||
269 | } |
||
14 | office | 270 | |
271 | /////////////////////////////////////////////////////////////////////////// |
||
272 | // Copyright (C) 2016 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 | public async Task<byte[]> GET(string URL) |
||
279 | { |
||
280 | try |
||
281 | { |
||
282 | using (var response = await HTTPClient.GetAsync(URL)) |
||
283 | { |
||
284 | return response.IsSuccessStatusCode ? await response.Content.ReadAsByteArrayAsync() : null; |
||
285 | } |
||
286 | } |
||
287 | catch (Exception) |
||
288 | { |
||
289 | return null; |
||
290 | } |
||
291 | } |
||
292 | |||
293 | public void Dispose() |
||
294 | { |
||
295 | if (HTTPClient != null) |
||
296 | HTTPClient.Dispose(); |
||
297 | } |
||
10 | office | 298 | } |
299 | } |