/
HttpRequest.cs
356 lines (300 loc) · 13.9 KB
/
HttpRequest.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
using System;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Threading;
using MNIX.Webservices;
namespace MNIX.HttpRequester
{
/// <summary>
/// Classe que representa uma requisição HTTP que pode enviar arquivos, dados simples e o misto dos 2
/// </summary>
/// <owner>Mnix-Victor</owner>
public class HttpRequest : IHttpRequest
{
public const int DEFAULT_BLOCK_SIZE = 4096;
private const uint DEFAULT_TIMEOUT = 100000;
private const long BEGIN_STREAM_POSITION = 0;
private object @lock = new object();
private static readonly Encoding DEFAULT_ENCODING = Encoding.UTF8;
private uint _mTimeout = DEFAULT_TIMEOUT;
private Encoding _mUsedEncoding = DEFAULT_ENCODING;
private Uri _mTargetUri;
private ParameterCollection mParameters = new ParameterCollection();
private HttpWebRequest mRunningRequest;
private UploadProgressChangedHandler mProgressChangedHandler;
private bool mIsAborted = false;
private bool _mIsRequestRunning = false;
/// <summary>
/// Quantidade de timeout que a requisição pode aguardar antes de disparar erro, em milisegundos
/// </summary>
/// <owner>Mnix-Victor</owner>
public uint Timeout
{
get { return _mTimeout; }
set { _mTimeout = value; }
}
/// <summary>
/// Encoding utilizado para fazer as requisições e converter o resultado delas em string novamente
/// </summary>
/// <owner>Mnix-Victor</owner>
public Encoding UsedEncoding
{
get { return _mUsedEncoding; }
set { _mUsedEncoding = value;}
}
/// <summary>
/// Uri utilizada para fazer esse request
/// </summary>
/// <owner>Mnix-Victor</owner>
public Uri TargetUri
{
get { return _mTargetUri; }
}
/// <summary>
/// Flag que diz se ja há uma requisição rodando com essa instância de HttpRequest
/// </summary>
/// <owner>Mnix-Victor</owner>
public bool IsRequestRunning
{
get { return _mIsRequestRunning; }
}
public HttpRequest(string targetUrl): this(new Uri(targetUrl)) { }
public HttpRequest(Uri targetUrl)
{
_mTargetUri = targetUrl;
//Parâmetros não podem ser enviados diretamente pela URL (Método GET), podendo causar problemas de conflito no momento de montar eles via request
if (_mTargetUri.Query != String.Empty)
{
throw new InvalidTargetURLException();
}
}
/// <summary>
/// Faz a requisição utilizando o método HTTP enviado via parâmetro e retornando o retorno em string
/// </summary>
/// <owner>Mnix-Victor</owner>
private Stream MakeRequest(HttpMethod httpMethod, UploadProgressChangedHandler progressChangedHandler)
{
lock (@lock)
{
HttpMethodAdapter methodAdapter = HttpMethodAdapterFactory.GetAdapter(httpMethod, this.mParameters, this.UsedEncoding);
mRunningRequest = (HttpWebRequest)HttpWebRequest.Create(methodAdapter.MountRequestUri(this._mTargetUri));
if (mIsAborted)
{
this.Abort();
this.mIsAborted = false;
}
mRunningRequest.Method = httpMethod.ToString();
mRunningRequest.Timeout = (int)this.Timeout;
mRunningRequest.ContentType = methodAdapter.MountContentType();
if (methodAdapter is HttpMethodAdapterFactory.BasePOSTHttpMethodAdapter)
{
this.mProgressChangedHandler = progressChangedHandler;
HttpMethodAdapterFactory.BasePOSTHttpMethodAdapter postHttpAdapter = (HttpMethodAdapterFactory.BasePOSTHttpMethodAdapter)methodAdapter;
postHttpAdapter.UploadProgressChangedEventHandler = SendProgressChangedNotification;
mRunningRequest.AllowWriteStreamBuffering = false;
mRunningRequest.ContentLength = postHttpAdapter.CachedContentSize;
//Pega o stream de saida para ser escrito na requsição
Stream writeableRequestStream = mRunningRequest.GetRequestStream();
postHttpAdapter.WriteRequestStream(writeableRequestStream);
writeableRequestStream.Close();
}
WebResponse response = mRunningRequest.GetResponse();
Stream responseStream = response.GetResponseStream();
return responseStream;
}
}
/// <summary>
/// Dispara o delegate de mudança de progresso no upload
/// </summary>
/// <owner>Mnix-Victor</owner>
private void SendProgressChangedNotification(long bytesAlreadySent, long totalBytesToSend)
{
if (mProgressChangedHandler != null)
{
mProgressChangedHandler.Invoke(this, bytesAlreadySent, totalBytesToSend);
}
}
/// <summary>
/// Pega o stream de response (saída) e monta a string de retorno, retornando ela
/// </summary>
/// <owner>Mnix-Victor</owner>
private string MountReturnString(Stream stream)
{
StringBuilder resultStringBuilder = new StringBuilder();
int readBytes;
byte[] totalBuffer = new byte[DEFAULT_BLOCK_SIZE];
while ((readBytes = stream.Read(totalBuffer, 0, DEFAULT_BLOCK_SIZE)) != 0)
{
resultStringBuilder.Append(UsedEncoding.GetString(totalBuffer, 0, readBytes));
}
return resultStringBuilder.ToString();
}
/// <summary>
/// Seta um valor para o parâmetro de nome enviado, a partir de um tipo
/// Se o parâmetro já existe, o valor dele é substituído
/// OBS: Um parâmetro de um tipo não pode ser mudado para outro tipo, disparando a exceção WrongParameterTypeException
/// </summary>
/// <owner>Mnix-Victor</owner>
private void SetParameter(string parameterName, string parameterValue, ParameterType parameterType)
{
Parameter parameterWithName = mParameters.FirstOrDefault((parameter) => (parameter.ParameterName == parameterName));
if (parameterWithName != null)
{
//Caso o parâmetro já exista só alterar o valor
//Não é possivel alterar um parâmetro já existe de outro tipo
if (parameterWithName.ParameterType != parameterType)
{
throw new WrongParameterTypeException(parameterName, parameterWithName.ParameterType, parameterType);
}
parameterWithName.ParameterValue = parameterValue;
}
else
{
//Se o parâmetro ainda não existe, crio um novo
Parameter newParameter = new Parameter(parameterName, parameterValue, parameterType);
newParameter.ParameterValue = parameterValue;
mParameters.AddParameter(newParameter);
}
}
/// <summary>
/// Trata os erros enviados pela exception e disparada o delegate de erro
/// </summary>
/// <owner>Mnix-Victor</owner>
private void DiagnoseRequestErrors(WebException exception, ErrorResultHandler errorResultHandler)
{
string errorOutput = String.Empty;
WebResponse response = exception.Response;
if (response != null)
{
StreamReader errorStreamReader = new StreamReader(exception.Response.GetResponseStream());
errorOutput = errorStreamReader.ReadToEnd();
}
errorResultHandler.Invoke(this, exception, errorOutput);
}
/// <summary>
/// Seta um valor para um parâmetro de nome enviado
/// OBS: Utilizar somente para parâmetros simples, para arquivos utilize o SetFile
/// </summary>
/// <owner>Mnix-Victor</owner>
public void SetParameter(string parameterName, string parameterValue)
{
SetParameter(parameterName, parameterValue, ParameterType.Value);
}
/// <summary>
/// Seta um arquivo para ser enviado com um nome de parâmetro
/// OBS: Enviar o caminho completo do arquivo
/// DÚVIDA: Pedir string ou FileInfo (forçando o programador a realmente enviar um arquivo)?
/// </summary>
/// <owner>Mnix-Victor</owner>
public void SetFile(string parameterName, string fileAddress)
{
SetParameter(parameterName, fileAddress, ParameterType.File);
}
/// <summary>
/// Executa a requisição de modo síncrono utilizando o método HTTP enviado e retorna a string de response
/// OBS: Utilizar o bloco try/catch para chamar esse método sempre que possível
/// </summary>
/// <param name="method">Método HTTP a ser utilizado para fazer a requisição</param>
/// <owner>Mnix-Victor</owner>
public string Execute(HttpMethod method)
{
Stream responseStream = MakeRequest(method, null);
string responseString = MountReturnString(responseStream);
responseStream.Close();
return responseString;
}
/// <summary>
/// Executa a requisição de modo assíncrono utilizando o método HTTP enviado e retorna o resultado a partir dos delegates de callback. Esse overload tem suporte a track de porcentagem de upload
/// Nesse overload também o resultado é retornado a partir de um Stream
/// </summary>
/// <param name="method">Método HTTP a ser utilizado para fazer a requisição</param>
/// <param name="sucessResultWithStreamHandler">Handler chamado quando houver sucesso na requisição</param>
/// <param name="progressChangedHandler">Handler chamado para notificar o progresso de upload da requisição</param>
/// <param name="errorResultHandler">Handler chamado quando houver erro na requisição</param>
/// <owner>Mnix-Victor</owner>
public void ExecuteAsync(HttpMethod method, SucessResultWithStreamHandler sucessResultWithStreamHandler, UploadProgressChangedHandler progressChangedHandler, ErrorResultHandler errorResultHandler)
{
if (method == HttpMethod.GET && progressChangedHandler != null)
{
throw new InvalidHTTPMethodForProgressTrackingException(method);
}
this._mIsRequestRunning = true;
Thread newThread = new Thread(delegate()
{
try
{
Stream responseStream = MakeRequest(method, progressChangedHandler);
sucessResultWithStreamHandler.Invoke(this, responseStream);
responseStream.Close();
}
catch (WebException webException)
{
this.DiagnoseRequestErrors(webException, errorResultHandler);
}
finally
{
this._mIsRequestRunning = false;
}
});
newThread.Start();
}
/// <summary>
/// Executa a requisição de modo assíncrono utilizando o método HTTP enviado e retorna o resultado a partir dos delegates de callback. Esse overload tem suporte a track de porcentagem de upload
/// </summary>
/// <param name="method">Método HTTP a ser utilizado para fazer a requisição</param>
/// <param name="sucessResultWithStreamHandler">Handler chamado quando houver sucesso na requisição</param>
/// <param name="progressChangedHandler">Handler chamado para notificar o progresso de upload da requisição</param>
/// <param name="errorResultHandler">Handler chamado quando houver erro na requisição</param>
/// <owner>Mnix-Victor</owner>
public void ExecuteAsync(HttpMethod method, SuccessResultHandler successResultHandler, UploadProgressChangedHandler progressChangedHandler, ErrorResultHandler errorResultHandler)
{
if (method == HttpMethod.GET && progressChangedHandler != null)
{
throw new InvalidHTTPMethodForProgressTrackingException(method);
}
this._mIsRequestRunning = true;
Thread newThread = new Thread(delegate()
{
try
{
Stream responseStream = MakeRequest(method, progressChangedHandler);
string resultString = MountReturnString(responseStream);
responseStream.Close();
successResultHandler.Invoke(this, resultString);
}
catch (WebException webException)
{
this.DiagnoseRequestErrors(webException, errorResultHandler);
}
finally
{
this._mIsRequestRunning = false;
}
});
newThread.Start();
}
/// <summary>
/// Cancela a requisição rodando
/// </summary>
/// <owner>Mnix-Victor</owner>
public void Abort()
{
if (!this.IsRequestRunning)
{
throw new RequestAlreadyStoppedException(this);
}
if (mRunningRequest == null)
{
//Flag de correção utilizada quando o request não foi iniciado ainda (utilização em modo assíncrono)
this.mIsAborted = true;
}
else
{
mRunningRequest.Abort();
this._mIsRequestRunning = false;
}
}
}
}