root / trunk / Pithos.Network / docs / restclient.html @ 1caef52e
History | View | Annotate | Download (17.3 kB)
1 | 1caef52e | Panagiotis Kanavos | <!DOCTYPE html />
|
---|---|---|---|
2 | 1caef52e | Panagiotis Kanavos | |
3 | 1caef52e | Panagiotis Kanavos | <html>
|
4 | 1caef52e | Panagiotis Kanavos | <head>
|
5 | 1caef52e | Panagiotis Kanavos | <title>RestClient.cs</title> |
6 | 1caef52e | Panagiotis Kanavos | <meta http-equiv="content-type" content="text/html; charset=UTF-8" /> |
7 | 1caef52e | Panagiotis Kanavos | <link href="focco.css" rel="stylesheet" media="all" type="text/css" /> |
8 | 1caef52e | Panagiotis Kanavos | <script src="prettify.js" type="text/javascript"></script> |
9 | 1caef52e | Panagiotis Kanavos | </head>
|
10 | 1caef52e | Panagiotis Kanavos | <body onload="prettyPrint()"> |
11 | 1caef52e | Panagiotis Kanavos | <div id="container"> |
12 | 1caef52e | Panagiotis Kanavos | <div id="background"></div> |
13 | 1caef52e | Panagiotis Kanavos | <div id="jump_to"> |
14 | 1caef52e | Panagiotis Kanavos | Jump To …
|
15 | 1caef52e | Panagiotis Kanavos | <div id="jump_wrapper"> |
16 | 1caef52e | Panagiotis Kanavos | <div id="jump_page"> |
17 | 1caef52e | Panagiotis Kanavos | <a class="source" href="cloudfilesclient.html"> |
18 | 1caef52e | Panagiotis Kanavos | CloudFilesClient.cs |
19 | 1caef52e | Panagiotis Kanavos | </a>
|
20 | 1caef52e | Panagiotis Kanavos | <a class="source" href="restclient.html"> |
21 | 1caef52e | Panagiotis Kanavos | RestClient.cs |
22 | 1caef52e | Panagiotis Kanavos | </a>
|
23 | 1caef52e | Panagiotis Kanavos | <a class="source" href="timeoutretrycondition.html"> |
24 | 1caef52e | Panagiotis Kanavos | TimeoutRetryCondition.cs |
25 | 1caef52e | Panagiotis Kanavos | </a>
|
26 | 1caef52e | Panagiotis Kanavos | <a class="source" href="properties/assemblyinfo.html"> |
27 | 1caef52e | Panagiotis Kanavos | Properties\AssemblyInfo.cs |
28 | 1caef52e | Panagiotis Kanavos | </a>
|
29 | 1caef52e | Panagiotis Kanavos | </div>
|
30 | 1caef52e | Panagiotis Kanavos | </div>
|
31 | 1caef52e | Panagiotis Kanavos | </div>
|
32 | 1caef52e | Panagiotis Kanavos | <table cellpadding="0" cellspacing="0"> |
33 | 1caef52e | Panagiotis Kanavos | <thead>
|
34 | 1caef52e | Panagiotis Kanavos | <tr>
|
35 | 1caef52e | Panagiotis Kanavos | <th class="docs"> |
36 | 1caef52e | Panagiotis Kanavos | <h1>RestClient.cs</h1> |
37 | 1caef52e | Panagiotis Kanavos | </th>
|
38 | 1caef52e | Panagiotis Kanavos | <th class="code"></th> |
39 | 1caef52e | Panagiotis Kanavos | </tr>
|
40 | 1caef52e | Panagiotis Kanavos | </thead>
|
41 | 1caef52e | Panagiotis Kanavos | <tbody>
|
42 | 1caef52e | Panagiotis Kanavos | <tr id="section_1"> |
43 | 1caef52e | Panagiotis Kanavos | <td class="docs"> |
44 | 1caef52e | Panagiotis Kanavos | <div class="pilwrap"> |
45 | 1caef52e | Panagiotis Kanavos | <a class="pilcrow" href="#section_1">¶</a> |
46 | 1caef52e | Panagiotis Kanavos | </div>
|
47 | 1caef52e | Panagiotis Kanavos | <hr /> |
48 | 1caef52e | Panagiotis Kanavos | |
49 | 1caef52e | Panagiotis Kanavos | <p><copyright file="RestClient.cs" company="Microsoft"> |
50 | 1caef52e | Panagiotis Kanavos | TODO: Update copyright text.</p>
|
51 | 1caef52e | Panagiotis Kanavos | |
52 | 1caef52e | Panagiotis Kanavos | <h2></copyright></h2> |
53 | 1caef52e | Panagiotis Kanavos | |
54 | 1caef52e | Panagiotis Kanavos | </td>
|
55 | 1caef52e | Panagiotis Kanavos | <td class="code"> |
56 | 1caef52e | Panagiotis Kanavos | <pre><code class='prettyprint'> |
57 | 1caef52e | Panagiotis Kanavos | using System.Collections.Specialized; |
58 | 1caef52e | Panagiotis Kanavos | using System.Diagnostics; |
59 | 1caef52e | Panagiotis Kanavos | using System.Diagnostics.Contracts; |
60 | 1caef52e | Panagiotis Kanavos | using System.IO; |
61 | 1caef52e | Panagiotis Kanavos | using System.Net; |
62 | 1caef52e | Panagiotis Kanavos | using System.Runtime.Serialization; |
63 | 1caef52e | Panagiotis Kanavos | using System.Threading.Tasks; |
64 | 1caef52e | Panagiotis Kanavos | |
65 | 1caef52e | Panagiotis Kanavos | namespace Pithos.Network |
66 | 1caef52e | Panagiotis Kanavos | { |
67 | 1caef52e | Panagiotis Kanavos | using System; |
68 | 1caef52e | Panagiotis Kanavos | using System.Collections.Generic; |
69 | 1caef52e | Panagiotis Kanavos | using System.Linq; |
70 | 1caef52e | Panagiotis Kanavos | using System.Text; |
71 | 1caef52e | Panagiotis Kanavos | |
72 | 1caef52e | Panagiotis Kanavos | </code></pre> |
73 | 1caef52e | Panagiotis Kanavos | </td>
|
74 | 1caef52e | Panagiotis Kanavos | </tr>
|
75 | 1caef52e | Panagiotis Kanavos | <tr id="section_2"> |
76 | 1caef52e | Panagiotis Kanavos | <td class="docs"> |
77 | 1caef52e | Panagiotis Kanavos | <div class="pilwrap"> |
78 | 1caef52e | Panagiotis Kanavos | <a class="pilcrow" href="#section_2">¶</a> |
79 | 1caef52e | Panagiotis Kanavos | </div>
|
80 | 1caef52e | Panagiotis Kanavos | <p>/ <summary> |
81 | 1caef52e | Panagiotis Kanavos | / TODO: Update summary. |
82 | 1caef52e | Panagiotis Kanavos | / </summary></p> |
83 | 1caef52e | Panagiotis Kanavos | |
84 | 1caef52e | Panagiotis Kanavos | </td>
|
85 | 1caef52e | Panagiotis Kanavos | <td class="code"> |
86 | 1caef52e | Panagiotis Kanavos | <pre><code class='prettyprint'> public class RestClient:WebClient |
87 | 1caef52e | Panagiotis Kanavos | { |
88 | 1caef52e | Panagiotis Kanavos | public int Timeout { get; set; } |
89 | 1caef52e | Panagiotis Kanavos | |
90 | 1caef52e | Panagiotis Kanavos | public bool TimedOut { get; set; } |
91 | 1caef52e | Panagiotis Kanavos | |
92 | 1caef52e | Panagiotis Kanavos | public HttpStatusCode StatusCode { get; private set; } |
93 | 1caef52e | Panagiotis Kanavos | |
94 | 1caef52e | Panagiotis Kanavos | public string StatusDescription { get; set; } |
95 | 1caef52e | Panagiotis Kanavos | |
96 | 1caef52e | Panagiotis Kanavos | |
97 | 1caef52e | Panagiotis Kanavos | public int Retries { get; set; } |
98 | 1caef52e | Panagiotis Kanavos | |
99 | 1caef52e | Panagiotis Kanavos | private readonly Dictionary<string, string> _parameters=new Dictionary<string, string>(); |
100 | 1caef52e | Panagiotis Kanavos | public Dictionary<string, string> Parameters |
101 | 1caef52e | Panagiotis Kanavos | { |
102 | 1caef52e | Panagiotis Kanavos | get { return _parameters; } |
103 | 1caef52e | Panagiotis Kanavos | } |
104 | 1caef52e | Panagiotis Kanavos | |
105 | 1caef52e | Panagiotis Kanavos | public RestClient():base() |
106 | 1caef52e | Panagiotis Kanavos | { |
107 | 1caef52e | Panagiotis Kanavos | |
108 | 1caef52e | Panagiotis Kanavos | } |
109 | 1caef52e | Panagiotis Kanavos | |
110 | 1caef52e | Panagiotis Kanavos | |
111 | 1caef52e | Panagiotis Kanavos | public RestClient(RestClient other) |
112 | 1caef52e | Panagiotis Kanavos | : base() |
113 | 1caef52e | Panagiotis Kanavos | { |
114 | 1caef52e | Panagiotis Kanavos | CopyHeaders(other); |
115 | 1caef52e | Panagiotis Kanavos | Timeout = other.Timeout; |
116 | 1caef52e | Panagiotis Kanavos | Retries = other.Retries; |
117 | 1caef52e | Panagiotis Kanavos | BaseAddress = other.BaseAddress; |
118 | 1caef52e | Panagiotis Kanavos | |
119 | 1caef52e | Panagiotis Kanavos | foreach (var parameter in other.Parameters) |
120 | 1caef52e | Panagiotis Kanavos | { |
121 | 1caef52e | Panagiotis Kanavos | Parameters.Add(parameter.Key,parameter.Value); |
122 | 1caef52e | Panagiotis Kanavos | } |
123 | 1caef52e | Panagiotis Kanavos | |
124 | 1caef52e | Panagiotis Kanavos | this.Proxy = other.Proxy; |
125 | 1caef52e | Panagiotis Kanavos | } |
126 | 1caef52e | Panagiotis Kanavos | |
127 | 1caef52e | Panagiotis Kanavos | protected override WebRequest GetWebRequest(Uri address) |
128 | 1caef52e | Panagiotis Kanavos | { |
129 | 1caef52e | Panagiotis Kanavos | TimedOut = false; |
130 | 1caef52e | Panagiotis Kanavos | var webRequest = base.GetWebRequest(address); |
131 | 1caef52e | Panagiotis Kanavos | var request = webRequest as HttpWebRequest; |
132 | 1caef52e | Panagiotis Kanavos | if (IfModifiedSince.HasValue) |
133 | 1caef52e | Panagiotis Kanavos | request.IfModifiedSince = IfModifiedSince.Value; |
134 | 1caef52e | Panagiotis Kanavos | request.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip; |
135 | 1caef52e | Panagiotis Kanavos | if(Timeout>0)
|
136 | 1caef52e | Panagiotis Kanavos | request.Timeout = Timeout; |
137 | 1caef52e | Panagiotis Kanavos | return request; |
138 | 1caef52e | Panagiotis Kanavos | } |
139 | 1caef52e | Panagiotis Kanavos | |
140 | 1caef52e | Panagiotis Kanavos | public DateTime? IfModifiedSince { get; set; } |
141 | 1caef52e | Panagiotis Kanavos | |
142 | 1caef52e | Panagiotis Kanavos | protected override WebResponse GetWebResponse(WebRequest request, IAsyncResult result) |
143 | 1caef52e | Panagiotis Kanavos | { |
144 | 1caef52e | Panagiotis Kanavos | var response = (HttpWebResponse) base.GetWebResponse(request, result); |
145 | 1caef52e | Panagiotis Kanavos | StatusCode=response.StatusCode; |
146 | 1caef52e | Panagiotis Kanavos | StatusDescription=response.StatusDescription; |
147 | 1caef52e | Panagiotis Kanavos | return response; |
148 | 1caef52e | Panagiotis Kanavos | } |
149 | 1caef52e | Panagiotis Kanavos | |
150 | 1caef52e | Panagiotis Kanavos | |
151 | 1caef52e | Panagiotis Kanavos | |
152 | 1caef52e | Panagiotis Kanavos | protected override WebResponse GetWebResponse(WebRequest request) |
153 | 1caef52e | Panagiotis Kanavos | { |
154 | 1caef52e | Panagiotis Kanavos | try |
155 | 1caef52e | Panagiotis Kanavos | { |
156 | 1caef52e | Panagiotis Kanavos | var response = (HttpWebResponse)base.GetWebResponse(request); |
157 | 1caef52e | Panagiotis Kanavos | StatusCode = response.StatusCode; |
158 | 1caef52e | Panagiotis Kanavos | StatusDescription = response.StatusDescription; |
159 | 1caef52e | Panagiotis Kanavos | return response; |
160 | 1caef52e | Panagiotis Kanavos | } |
161 | 1caef52e | Panagiotis Kanavos | catch (WebException exc) |
162 | 1caef52e | Panagiotis Kanavos | { |
163 | 1caef52e | Panagiotis Kanavos | if (exc.Response!=null && exc.Response.ContentLength > 0) |
164 | 1caef52e | Panagiotis Kanavos | { |
165 | 1caef52e | Panagiotis Kanavos | string content = GetContent(exc.Response); |
166 | 1caef52e | Panagiotis Kanavos | Trace.TraceError(content); |
167 | 1caef52e | Panagiotis Kanavos | } |
168 | 1caef52e | Panagiotis Kanavos | throw; |
169 | 1caef52e | Panagiotis Kanavos | } |
170 | 1caef52e | Panagiotis Kanavos | } |
171 | 1caef52e | Panagiotis Kanavos | |
172 | 1caef52e | Panagiotis Kanavos | private static string GetContent(WebResponse webResponse) |
173 | 1caef52e | Panagiotis Kanavos | { |
174 | 1caef52e | Panagiotis Kanavos | string content; |
175 | 1caef52e | Panagiotis Kanavos | using (var stream = webResponse.GetResponseStream()) |
176 | 1caef52e | Panagiotis Kanavos | using (var reader = new StreamReader(stream)) |
177 | 1caef52e | Panagiotis Kanavos | { |
178 | 1caef52e | Panagiotis Kanavos | content = reader.ReadToEnd(); |
179 | 1caef52e | Panagiotis Kanavos | } |
180 | 1caef52e | Panagiotis Kanavos | return content; |
181 | 1caef52e | Panagiotis Kanavos | } |
182 | 1caef52e | Panagiotis Kanavos | |
183 | 1caef52e | Panagiotis Kanavos | public string DownloadStringWithRetry(string address,int retries=0) |
184 | 1caef52e | Panagiotis Kanavos | { |
185 | 1caef52e | Panagiotis Kanavos | if (address == null) |
186 | 1caef52e | Panagiotis Kanavos | throw new ArgumentNullException("address"); |
187 | 1caef52e | Panagiotis Kanavos | |
188 | 1caef52e | Panagiotis Kanavos | var actualAddress = GetActualAddress(address); |
189 | 1caef52e | Panagiotis Kanavos | |
190 | 1caef52e | Panagiotis Kanavos | TraceStart("GET",actualAddress); |
191 | 1caef52e | Panagiotis Kanavos | |
192 | 1caef52e | Panagiotis Kanavos | var actualRetries = (retries == 0) ? Retries : retries; |
193 | 1caef52e | Panagiotis Kanavos | |
194 | 1caef52e | Panagiotis Kanavos | |
195 | 1caef52e | Panagiotis Kanavos | |
196 | 1caef52e | Panagiotis Kanavos | var task = Retry(() =>
|
197 | 1caef52e | Panagiotis Kanavos | { |
198 | 1caef52e | Panagiotis Kanavos | var uriString = String.Join("/", BaseAddress.TrimEnd('/'), actualAddress); |
199 | 1caef52e | Panagiotis Kanavos | var content = base.DownloadString(uriString); |
200 | 1caef52e | Panagiotis Kanavos | |
201 | 1caef52e | Panagiotis Kanavos | if (StatusCode == HttpStatusCode.NoContent) |
202 | 1caef52e | Panagiotis Kanavos | return String.Empty; |
203 | 1caef52e | Panagiotis Kanavos | return content; |
204 | 1caef52e | Panagiotis Kanavos | |
205 | 1caef52e | Panagiotis Kanavos | }, actualRetries); |
206 | 1caef52e | Panagiotis Kanavos | |
207 | 1caef52e | Panagiotis Kanavos | var result = task.Result; |
208 | 1caef52e | Panagiotis Kanavos | return result; |
209 | 1caef52e | Panagiotis Kanavos | } |
210 | 1caef52e | Panagiotis Kanavos | |
211 | 1caef52e | Panagiotis Kanavos | public void Head(string address,int retries=0) |
212 | 1caef52e | Panagiotis Kanavos | { |
213 | 1caef52e | Panagiotis Kanavos | RetryWithoutContent(address, retries, "HEAD"); |
214 | 1caef52e | Panagiotis Kanavos | } |
215 | 1caef52e | Panagiotis Kanavos | |
216 | 1caef52e | Panagiotis Kanavos | public void PutWithRetry(string address, int retries = 0) |
217 | 1caef52e | Panagiotis Kanavos | { |
218 | 1caef52e | Panagiotis Kanavos | RetryWithoutContent(address, retries, "PUT"); |
219 | 1caef52e | Panagiotis Kanavos | } |
220 | 1caef52e | Panagiotis Kanavos | |
221 | 1caef52e | Panagiotis Kanavos | public void DeleteWithRetry(string address,int retries=0) |
222 | 1caef52e | Panagiotis Kanavos | { |
223 | 1caef52e | Panagiotis Kanavos | RetryWithoutContent(address, retries, "DELETE"); |
224 | 1caef52e | Panagiotis Kanavos | } |
225 | 1caef52e | Panagiotis Kanavos | |
226 | 1caef52e | Panagiotis Kanavos | public string GetHeaderValue(string headerName) |
227 | 1caef52e | Panagiotis Kanavos | { |
228 | 1caef52e | Panagiotis Kanavos | var values=this.ResponseHeaders.GetValues(headerName); |
229 | 1caef52e | Panagiotis Kanavos | if (values == null) |
230 | 1caef52e | Panagiotis Kanavos | throw new WebException(String.Format("The {0} header is missing", headerName)); |
231 | 1caef52e | Panagiotis Kanavos | else |
232 | 1caef52e | Panagiotis Kanavos | return values[0]; |
233 | 1caef52e | Panagiotis Kanavos | } |
234 | 1caef52e | Panagiotis Kanavos | |
235 | 1caef52e | Panagiotis Kanavos | private void RetryWithoutContent(string address, int retries, string method) |
236 | 1caef52e | Panagiotis Kanavos | { |
237 | 1caef52e | Panagiotis Kanavos | if (address == null) |
238 | 1caef52e | Panagiotis Kanavos | throw new ArgumentNullException("address"); |
239 | 1caef52e | Panagiotis Kanavos | |
240 | 1caef52e | Panagiotis Kanavos | var actualAddress = GetActualAddress(address); |
241 | 1caef52e | Panagiotis Kanavos | var actualRetries = (retries == 0) ? Retries : retries; |
242 | 1caef52e | Panagiotis Kanavos | |
243 | 1caef52e | Panagiotis Kanavos | var task = Retry(() =>
|
244 | 1caef52e | Panagiotis Kanavos | { |
245 | 1caef52e | Panagiotis Kanavos | var uriString = String.Join("/",BaseAddress ,actualAddress); |
246 | 1caef52e | Panagiotis Kanavos | var uri = new Uri(uriString); |
247 | 1caef52e | Panagiotis Kanavos | var request = GetWebRequest(uri); |
248 | 1caef52e | Panagiotis Kanavos | request.Method = method; |
249 | 1caef52e | Panagiotis Kanavos | if (ResponseHeaders!=null) |
250 | 1caef52e | Panagiotis Kanavos | ResponseHeaders.Clear(); |
251 | 1caef52e | Panagiotis Kanavos | |
252 | 1caef52e | Panagiotis Kanavos | TraceStart(method, uriString); |
253 | 1caef52e | Panagiotis Kanavos | |
254 | 1caef52e | Panagiotis Kanavos | var response = (HttpWebResponse)GetWebResponse(request); |
255 | 1caef52e | Panagiotis Kanavos | StatusCode = response.StatusCode; |
256 | 1caef52e | Panagiotis Kanavos | StatusDescription = response.StatusDescription; |
257 | 1caef52e | Panagiotis Kanavos | |
258 | 1caef52e | Panagiotis Kanavos | |
259 | 1caef52e | Panagiotis Kanavos | return 0; |
260 | 1caef52e | Panagiotis Kanavos | }, actualRetries); |
261 | 1caef52e | Panagiotis Kanavos | |
262 | 1caef52e | Panagiotis Kanavos | task.Wait(); |
263 | 1caef52e | Panagiotis Kanavos | } |
264 | 1caef52e | Panagiotis Kanavos | |
265 | 1caef52e | Panagiotis Kanavos | /*private string RetryWithContent(string address, int retries, string method) |
266 | 1caef52e | Panagiotis Kanavos | { |
267 | 1caef52e | Panagiotis Kanavos | if (address == null) |
268 | 1caef52e | Panagiotis Kanavos | throw new ArgumentNullException("address"); |
269 | 1caef52e | Panagiotis Kanavos | |
270 | 1caef52e | Panagiotis Kanavos | var actualAddress = GetActualAddress(address); |
271 | 1caef52e | Panagiotis Kanavos | var actualRetries = (retries == 0) ? Retries : retries; |
272 | 1caef52e | Panagiotis Kanavos | |
273 | 1caef52e | Panagiotis Kanavos | var task = Retry(() =>
|
274 | 1caef52e | Panagiotis Kanavos | { |
275 | 1caef52e | Panagiotis Kanavos | var uriString = String.Join("/",BaseAddress ,actualAddress); |
276 | 1caef52e | Panagiotis Kanavos | var uri = new Uri(uriString); |
277 | 1caef52e | Panagiotis Kanavos | |
278 | 1caef52e | Panagiotis Kanavos | var request = GetWebRequest(uri); |
279 | 1caef52e | Panagiotis Kanavos | request.Method = method; |
280 | 1caef52e | Panagiotis Kanavos | |
281 | 1caef52e | Panagiotis Kanavos | if (ResponseHeaders!=null) |
282 | 1caef52e | Panagiotis Kanavos | ResponseHeaders.Clear(); |
283 | 1caef52e | Panagiotis Kanavos | |
284 | 1caef52e | Panagiotis Kanavos | TraceStart(method, uriString); |
285 | 1caef52e | Panagiotis Kanavos | |
286 | 1caef52e | Panagiotis Kanavos | var getResponse = request.GetResponseAsync(); |
287 | 1caef52e | Panagiotis Kanavos | |
288 | 1caef52e | Panagiotis Kanavos | var setStatus= getResponse.ContinueWith(t =>
|
289 | 1caef52e | Panagiotis Kanavos | { |
290 | 1caef52e | Panagiotis Kanavos | var response = (HttpWebResponse)t.Result; |
291 | 1caef52e | Panagiotis Kanavos | StatusCode = response.StatusCode; |
292 | 1caef52e | Panagiotis Kanavos | StatusDescription = response.StatusDescription; |
293 | 1caef52e | Panagiotis Kanavos | return response; |
294 | 1caef52e | Panagiotis Kanavos | }); |
295 | 1caef52e | Panagiotis Kanavos | |
296 | 1caef52e | Panagiotis Kanavos | var getData = setStatus.ContinueWith(t =>
|
297 | 1caef52e | Panagiotis Kanavos | { |
298 | 1caef52e | Panagiotis Kanavos | var response = t.Result; |
299 | 1caef52e | Panagiotis Kanavos | return response.GetResponseStream() |
300 | 1caef52e | Panagiotis Kanavos | .ReadAllBytesAsync(); |
301 | 1caef52e | Panagiotis Kanavos | }).Unwrap(); |
302 | 1caef52e | Panagiotis Kanavos | |
303 | 1caef52e | Panagiotis Kanavos | var data = getData.Result; |
304 | 1caef52e | Panagiotis Kanavos | var content=Encoding.UTF8.GetString(data); |
305 | 1caef52e | Panagiotis Kanavos | |
306 | 1caef52e | Panagiotis Kanavos | </code></pre> |
307 | 1caef52e | Panagiotis Kanavos | </td>
|
308 | 1caef52e | Panagiotis Kanavos | </tr>
|
309 | 1caef52e | Panagiotis Kanavos | <tr id="section_3"> |
310 | 1caef52e | Panagiotis Kanavos | <td class="docs"> |
311 | 1caef52e | Panagiotis Kanavos | <div class="pilwrap"> |
312 | 1caef52e | Panagiotis Kanavos | <a class="pilcrow" href="#section_3">¶</a> |
313 | 1caef52e | Panagiotis Kanavos | </div>
|
314 | 1caef52e | Panagiotis Kanavos | <pre><code> var response = (HttpWebResponse)GetWebResponse(request); |
315 | 1caef52e | Panagiotis Kanavos | </code></pre> |
316 | 1caef52e | Panagiotis Kanavos | |
317 | 1caef52e | Panagiotis Kanavos | </td>
|
318 | 1caef52e | Panagiotis Kanavos | <td class="code"> |
319 | 1caef52e | Panagiotis Kanavos | <pre><code class='prettyprint'> |
320 | 1caef52e | Panagiotis Kanavos | |
321 | 1caef52e | Panagiotis Kanavos | /* |
322 | 1caef52e | Panagiotis Kanavos | StatusCode = response.StatusCode; |
323 | 1caef52e | Panagiotis Kanavos | StatusDescription = response.StatusDescription; |
324 | 1caef52e | Panagiotis Kanavos | #1# |
325 | 1caef52e | Panagiotis Kanavos | |
326 | 1caef52e | Panagiotis Kanavos | |
327 | 1caef52e | Panagiotis Kanavos | return content; |
328 | 1caef52e | Panagiotis Kanavos | }, actualRetries); |
329 | 1caef52e | Panagiotis Kanavos | |
330 | 1caef52e | Panagiotis Kanavos | return task.Result; |
331 | 1caef52e | Panagiotis Kanavos | }*/ |
332 | 1caef52e | Panagiotis Kanavos | |
333 | 1caef52e | Panagiotis Kanavos | private static void TraceStart(string method, string actualAddress) |
334 | 1caef52e | Panagiotis Kanavos | { |
335 | 1caef52e | Panagiotis Kanavos | Trace.WriteLine(String.Format("[{0}] {1} {2}", method, DateTime.Now, actualAddress)); |
336 | 1caef52e | Panagiotis Kanavos | } |
337 | 1caef52e | Panagiotis Kanavos | |
338 | 1caef52e | Panagiotis Kanavos | private string GetActualAddress(string address) |
339 | 1caef52e | Panagiotis Kanavos | { |
340 | 1caef52e | Panagiotis Kanavos | if (Parameters.Count == 0) |
341 | 1caef52e | Panagiotis Kanavos | return address; |
342 | 1caef52e | Panagiotis Kanavos | var addressBuilder=new StringBuilder(address); |
343 | 1caef52e | Panagiotis Kanavos | |
344 | 1caef52e | Panagiotis Kanavos | bool isFirst = true; |
345 | 1caef52e | Panagiotis Kanavos | foreach (var parameter in Parameters) |
346 | 1caef52e | Panagiotis Kanavos | { |
347 | 1caef52e | Panagiotis Kanavos | if(isFirst) |
348 | 1caef52e | Panagiotis Kanavos | addressBuilder.AppendFormat("?{0}={1}", parameter.Key, parameter.Value); |
349 | 1caef52e | Panagiotis Kanavos | else |
350 | 1caef52e | Panagiotis Kanavos | addressBuilder.AppendFormat("&{0}={1}", parameter.Key, parameter.Value); |
351 | 1caef52e | Panagiotis Kanavos | isFirst = false; |
352 | 1caef52e | Panagiotis Kanavos | } |
353 | 1caef52e | Panagiotis Kanavos | return addressBuilder.ToString(); |
354 | 1caef52e | Panagiotis Kanavos | } |
355 | 1caef52e | Panagiotis Kanavos | |
356 | 1caef52e | Panagiotis Kanavos | public string DownloadStringWithRetry(Uri address,int retries=0) |
357 | 1caef52e | Panagiotis Kanavos | { |
358 | 1caef52e | Panagiotis Kanavos | if (address == null) |
359 | 1caef52e | Panagiotis Kanavos | throw new ArgumentNullException("address"); |
360 | 1caef52e | Panagiotis Kanavos | |
361 | 1caef52e | Panagiotis Kanavos | var actualRetries = (retries == 0) ? Retries : retries; |
362 | 1caef52e | Panagiotis Kanavos | var task = Retry(() =>
|
363 | 1caef52e | Panagiotis Kanavos | { |
364 | 1caef52e | Panagiotis Kanavos | var content = base.DownloadString(address); |
365 | 1caef52e | Panagiotis Kanavos | |
366 | 1caef52e | Panagiotis Kanavos | if (StatusCode == HttpStatusCode.NoContent) |
367 | 1caef52e | Panagiotis Kanavos | return String.Empty; |
368 | 1caef52e | Panagiotis Kanavos | return content; |
369 | 1caef52e | Panagiotis Kanavos | |
370 | 1caef52e | Panagiotis Kanavos | }, actualRetries); |
371 | 1caef52e | Panagiotis Kanavos | |
372 | 1caef52e | Panagiotis Kanavos | var result = task.Result; |
373 | 1caef52e | Panagiotis Kanavos | return result; |
374 | 1caef52e | Panagiotis Kanavos | } |
375 | 1caef52e | Panagiotis Kanavos | |
376 | 1caef52e | Panagiotis Kanavos | |
377 | 1caef52e | Panagiotis Kanavos | </code></pre> |
378 | 1caef52e | Panagiotis Kanavos | </td>
|
379 | 1caef52e | Panagiotis Kanavos | </tr>
|
380 | 1caef52e | Panagiotis Kanavos | <tr id="section_4"> |
381 | 1caef52e | Panagiotis Kanavos | <td class="docs"> |
382 | 1caef52e | Panagiotis Kanavos | <div class="pilwrap"> |
383 | 1caef52e | Panagiotis Kanavos | <a class="pilcrow" href="#section_4">¶</a> |
384 | 1caef52e | Panagiotis Kanavos | </div>
|
385 | 1caef52e | Panagiotis Kanavos | <p>/ <summary> |
386 | 1caef52e | Panagiotis Kanavos | / Copies headers from another RestClient |
387 | 1caef52e | Panagiotis Kanavos | / </summary>
|
388 | 1caef52e | Panagiotis Kanavos | / <param name="source">The RestClient from which the headers are copied</param></p> |
389 | 1caef52e | Panagiotis Kanavos | |
390 | 1caef52e | Panagiotis Kanavos | </td>
|
391 | 1caef52e | Panagiotis Kanavos | <td class="code"> |
392 | 1caef52e | Panagiotis Kanavos | <pre><code class='prettyprint'> public void CopyHeaders(RestClient source) |
393 | 1caef52e | Panagiotis Kanavos | { |
394 | 1caef52e | Panagiotis Kanavos | Contract.Requires(source != null, "source can't be null"); |
395 | 1caef52e | Panagiotis Kanavos | if (source == null) |
396 | 1caef52e | Panagiotis Kanavos | throw new ArgumentNullException("source", "source can't be null"); |
397 | 1caef52e | Panagiotis Kanavos | CopyHeaders(source.Headers,Headers); |
398 | 1caef52e | Panagiotis Kanavos | } |
399 | 1caef52e | Panagiotis Kanavos | |
400 | 1caef52e | Panagiotis Kanavos | </code></pre> |
401 | 1caef52e | Panagiotis Kanavos | </td>
|
402 | 1caef52e | Panagiotis Kanavos | </tr>
|
403 | 1caef52e | Panagiotis Kanavos | <tr id="section_5"> |
404 | 1caef52e | Panagiotis Kanavos | <td class="docs"> |
405 | 1caef52e | Panagiotis Kanavos | <div class="pilwrap"> |
406 | 1caef52e | Panagiotis Kanavos | <a class="pilcrow" href="#section_5">¶</a> |
407 | 1caef52e | Panagiotis Kanavos | </div>
|
408 | 1caef52e | Panagiotis Kanavos | <p>/ <summary> |
409 | 1caef52e | Panagiotis Kanavos | / Copies headers from one header collection to another |
410 | 1caef52e | Panagiotis Kanavos | / </summary>
|
411 | 1caef52e | Panagiotis Kanavos | / <param name="source">The source collection from which the headers are copied</param> |
412 | 1caef52e | Panagiotis Kanavos | / <param name="target">The target collection to which the headers are copied</param></p> |
413 | 1caef52e | Panagiotis Kanavos | |
414 | 1caef52e | Panagiotis Kanavos | </td>
|
415 | 1caef52e | Panagiotis Kanavos | <td class="code"> |
416 | 1caef52e | Panagiotis Kanavos | <pre><code class='prettyprint'> public static void CopyHeaders(WebHeaderCollection source,WebHeaderCollection target) |
417 | 1caef52e | Panagiotis Kanavos | { |
418 | 1caef52e | Panagiotis Kanavos | Contract.Requires(source != null, "source can't be null"); |
419 | 1caef52e | Panagiotis Kanavos | Contract.Requires(target != null, "target can't be null"); |
420 | 1caef52e | Panagiotis Kanavos | if (source == null) |
421 | 1caef52e | Panagiotis Kanavos | throw new ArgumentNullException("source", "source can't be null"); |
422 | 1caef52e | Panagiotis Kanavos | if (target == null) |
423 | 1caef52e | Panagiotis Kanavos | throw new ArgumentNullException("target", "target can't be null"); |
424 | 1caef52e | Panagiotis Kanavos | for (int i = 0; i < source.Count; i++)
|
425 | 1caef52e | Panagiotis Kanavos | { |
426 | 1caef52e | Panagiotis Kanavos | target.Add(source.GetKey(i), source[i]); |
427 | 1caef52e | Panagiotis Kanavos | } |
428 | 1caef52e | Panagiotis Kanavos | } |
429 | 1caef52e | Panagiotis Kanavos | |
430 | 1caef52e | Panagiotis Kanavos | public void AssertStatusOK(string message) |
431 | 1caef52e | Panagiotis Kanavos | { |
432 | 1caef52e | Panagiotis Kanavos | if (StatusCode >= HttpStatusCode.BadRequest)
|
433 | 1caef52e | Panagiotis Kanavos | throw new WebException(String.Format("{0} with code {1} - {2}", message, StatusCode, StatusDescription)); |
434 | 1caef52e | Panagiotis Kanavos | } |
435 | 1caef52e | Panagiotis Kanavos | |
436 | 1caef52e | Panagiotis Kanavos | |
437 | 1caef52e | Panagiotis Kanavos | private Task<T> Retry<T>(Func<T> original, int retryCount, TaskCompletionSource<T> tcs = null) |
438 | 1caef52e | Panagiotis Kanavos | { |
439 | 1caef52e | Panagiotis Kanavos | if (tcs == null) |
440 | 1caef52e | Panagiotis Kanavos | tcs = new TaskCompletionSource<T>(); |
441 | 1caef52e | Panagiotis Kanavos | Task.Factory.StartNew(original).ContinueWith(_original =>
|
442 | 1caef52e | Panagiotis Kanavos | { |
443 | 1caef52e | Panagiotis Kanavos | if (!_original.IsFaulted) |
444 | 1caef52e | Panagiotis Kanavos | tcs.SetFromTask(_original); |
445 | 1caef52e | Panagiotis Kanavos | else |
446 | 1caef52e | Panagiotis Kanavos | { |
447 | 1caef52e | Panagiotis Kanavos | var e = _original.Exception.InnerException; |
448 | 1caef52e | Panagiotis Kanavos | var we = (e as WebException); |
449 | 1caef52e | Panagiotis Kanavos | if (we==null) |
450 | 1caef52e | Panagiotis Kanavos | tcs.SetException(e); |
451 | 1caef52e | Panagiotis Kanavos | else |
452 | 1caef52e | Panagiotis Kanavos | { |
453 | 1caef52e | Panagiotis Kanavos | var statusCode = GetStatusCode(we); |
454 | 1caef52e | Panagiotis Kanavos | |
455 | 1caef52e | Panagiotis Kanavos | </code></pre> |
456 | 1caef52e | Panagiotis Kanavos | </td>
|
457 | 1caef52e | Panagiotis Kanavos | </tr>
|
458 | 1caef52e | Panagiotis Kanavos | <tr id="section_6"> |
459 | 1caef52e | Panagiotis Kanavos | <td class="docs"> |
460 | 1caef52e | Panagiotis Kanavos | <div class="pilwrap"> |
461 | 1caef52e | Panagiotis Kanavos | <a class="pilcrow" href="#section_6">¶</a> |
462 | 1caef52e | Panagiotis Kanavos | </div>
|
463 | 1caef52e | Panagiotis Kanavos | <p>Return null for 404</p> |
464 | 1caef52e | Panagiotis Kanavos | |
465 | 1caef52e | Panagiotis Kanavos | </td>
|
466 | 1caef52e | Panagiotis Kanavos | <td class="code"> |
467 | 1caef52e | Panagiotis Kanavos | <pre><code class='prettyprint'> if (statusCode == HttpStatusCode.NotFound) |
468 | 1caef52e | Panagiotis Kanavos | tcs.SetResult(default(T)); |
469 | 1caef52e | Panagiotis Kanavos | </code></pre> |
470 | 1caef52e | Panagiotis Kanavos | </td>
|
471 | 1caef52e | Panagiotis Kanavos | </tr>
|
472 | 1caef52e | Panagiotis Kanavos | <tr id="section_7"> |
473 | 1caef52e | Panagiotis Kanavos | <td class="docs"> |
474 | 1caef52e | Panagiotis Kanavos | <div class="pilwrap"> |
475 | 1caef52e | Panagiotis Kanavos | <a class="pilcrow" href="#section_7">¶</a> |
476 | 1caef52e | Panagiotis Kanavos | </div>
|
477 | 1caef52e | Panagiotis Kanavos | <p>Retry for timeouts and service unavailable</p> |
478 | 1caef52e | Panagiotis Kanavos | |
479 | 1caef52e | Panagiotis Kanavos | </td>
|
480 | 1caef52e | Panagiotis Kanavos | <td class="code"> |
481 | 1caef52e | Panagiotis Kanavos | <pre><code class='prettyprint'> else if (we.Status == WebExceptionStatus.Timeout || |
482 | 1caef52e | Panagiotis Kanavos | (we.Status == WebExceptionStatus.ProtocolError && statusCode == HttpStatusCode.ServiceUnavailable)) |
483 | 1caef52e | Panagiotis Kanavos | { |
484 | 1caef52e | Panagiotis Kanavos | TimedOut = true; |
485 | 1caef52e | Panagiotis Kanavos | if (retryCount == 0) |
486 | 1caef52e | Panagiotis Kanavos | { |
487 | 1caef52e | Panagiotis Kanavos | Trace.TraceError("[ERROR] Timed out too many times. {0}\n", e); |
488 | 1caef52e | Panagiotis Kanavos | tcs.SetException(new RetryException("Timed out too many times.", e)); |
489 | 1caef52e | Panagiotis Kanavos | } |
490 | 1caef52e | Panagiotis Kanavos | else |
491 | 1caef52e | Panagiotis Kanavos | { |
492 | 1caef52e | Panagiotis Kanavos | Trace.TraceError( |
493 | 1caef52e | Panagiotis Kanavos | "[RETRY] Timed out after {0} ms. Will retry {1} more times\n{2}", Timeout, |
494 | 1caef52e | Panagiotis Kanavos | retryCount, e); |
495 | 1caef52e | Panagiotis Kanavos | Retry(original, retryCount - 1, tcs); |
496 | 1caef52e | Panagiotis Kanavos | } |
497 | 1caef52e | Panagiotis Kanavos | } |
498 | 1caef52e | Panagiotis Kanavos | else |
499 | 1caef52e | Panagiotis Kanavos | tcs.SetException(e); |
500 | 1caef52e | Panagiotis Kanavos | } |
501 | 1caef52e | Panagiotis Kanavos | }; |
502 | 1caef52e | Panagiotis Kanavos | }); |
503 | 1caef52e | Panagiotis Kanavos | return tcs.Task; |
504 | 1caef52e | Panagiotis Kanavos | } |
505 | 1caef52e | Panagiotis Kanavos | |
506 | 1caef52e | Panagiotis Kanavos | private HttpStatusCode GetStatusCode(WebException we) |
507 | 1caef52e | Panagiotis Kanavos | { |
508 | 1caef52e | Panagiotis Kanavos | var statusCode = HttpStatusCode.RequestTimeout; |
509 | 1caef52e | Panagiotis Kanavos | if (we.Response != null) |
510 | 1caef52e | Panagiotis Kanavos | { |
511 | 1caef52e | Panagiotis Kanavos | statusCode = ((HttpWebResponse) we.Response).StatusCode; |
512 | 1caef52e | Panagiotis Kanavos | this.StatusCode = statusCode; |
513 | 1caef52e | Panagiotis Kanavos | } |
514 | 1caef52e | Panagiotis Kanavos | return statusCode; |
515 | 1caef52e | Panagiotis Kanavos | } |
516 | 1caef52e | Panagiotis Kanavos | } |
517 | 1caef52e | Panagiotis Kanavos | |
518 | 1caef52e | Panagiotis Kanavos | public class RetryException:Exception |
519 | 1caef52e | Panagiotis Kanavos | { |
520 | 1caef52e | Panagiotis Kanavos | public RetryException() |
521 | 1caef52e | Panagiotis Kanavos | :base() |
522 | 1caef52e | Panagiotis Kanavos | { |
523 | 1caef52e | Panagiotis Kanavos | |
524 | 1caef52e | Panagiotis Kanavos | } |
525 | 1caef52e | Panagiotis Kanavos | |
526 | 1caef52e | Panagiotis Kanavos | public RetryException(string message) |
527 | 1caef52e | Panagiotis Kanavos | :base(message) |
528 | 1caef52e | Panagiotis Kanavos | { |
529 | 1caef52e | Panagiotis Kanavos | |
530 | 1caef52e | Panagiotis Kanavos | } |
531 | 1caef52e | Panagiotis Kanavos | |
532 | 1caef52e | Panagiotis Kanavos | public RetryException(string message,Exception innerException) |
533 | 1caef52e | Panagiotis Kanavos | :base(message,innerException) |
534 | 1caef52e | Panagiotis Kanavos | { |
535 | 1caef52e | Panagiotis Kanavos | |
536 | 1caef52e | Panagiotis Kanavos | } |
537 | 1caef52e | Panagiotis Kanavos | |
538 | 1caef52e | Panagiotis Kanavos | public RetryException(SerializationInfo info,StreamingContext context) |
539 | 1caef52e | Panagiotis Kanavos | :base(info,context) |
540 | 1caef52e | Panagiotis Kanavos | { |
541 | 1caef52e | Panagiotis Kanavos | |
542 | 1caef52e | Panagiotis Kanavos | } |
543 | 1caef52e | Panagiotis Kanavos | } |
544 | 1caef52e | Panagiotis Kanavos | } |
545 | 1caef52e | Panagiotis Kanavos | </code></pre> |
546 | 1caef52e | Panagiotis Kanavos | </td>
|
547 | 1caef52e | Panagiotis Kanavos | </tr>
|
548 | 1caef52e | Panagiotis Kanavos | </tbody>
|
549 | 1caef52e | Panagiotis Kanavos | </table>
|
550 | 1caef52e | Panagiotis Kanavos | </div>
|
551 | 1caef52e | Panagiotis Kanavos | </body>
|
552 | 1caef52e | Panagiotis Kanavos | </html> |