-
Notifications
You must be signed in to change notification settings - Fork 10
/
HttpHelper.cs.pp
2425 lines (2172 loc) · 84.7 KB
/
HttpHelper.cs.pp
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//-----------------------------------------------------------------------
// <copyright file="HttpHelper.cs.pp" company="The Outercurve Foundation">
// Copyright (c) 2011, The Outercurve Foundation.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>
// <author>Prabir Shrestha (prabir.me)</author>
// <website>https://github.com/facebook-csharp-sdk/http-helper</website>
//-----------------------------------------------------------------------
// Install-Package HttpHelper
//#define HTTPHELPER_PORTABLE_LIBRARY
//#define HTTPHELPER_TPL
//#define HTTPHELPER_HELPERS
//#define HTTPHELPER_STREAM
//#define HTTPHELPER_NO_URLENCODING
//#define HTTPHELPER_HTMLENCODING
//#define HTTPHELPER_NO_HTTPBASIC_AUTHENTICATION
//#define HTTPHELPER_NO_OAUTH
//#define HTTPHELPER_PUBLIC
#if NETFX_CORE
#define HTTPHELPER_TPL
#endif
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.IO;
using System.Net;
using System.Text;
using System.Threading;
#if HTTPHELPER_TPL
using System.Threading.Tasks;
#endif
namespace $rootnamespace$
{
/// <summary>
/// Represents the http web request wrapper.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public class HttpWebRequestWrapper
{
/// <summary>
/// The http web request.
/// </summary>
private readonly HttpWebRequest _httpWebRequest;
/// <summary>
/// Initializes a new instance of the <see cref="HttpWebRequestWrapper"/> class.
/// </summary>
protected HttpWebRequestWrapper()
{
}
/// <summary>
/// Initializes a new instance of the <see cref="HttpWebRequestWrapper"/> class.
/// </summary>
/// <param name="httpWebRequest">
/// The http web request.
/// </param>
public HttpWebRequestWrapper(HttpWebRequest httpWebRequest)
{
if (httpWebRequest == null)
throw new ArgumentNullException("httpWebRequest");
_httpWebRequest = httpWebRequest;
}
/// <summary>
/// Gets or sets the http method.
/// </summary>
public virtual string Method
{
get { return _httpWebRequest.Method; }
set { _httpWebRequest.Method = value; }
}
/// <summary>
/// Gets or sets the content type.
/// </summary>
public virtual string ContentType
{
get { return _httpWebRequest.ContentType; }
set { _httpWebRequest.ContentType = value; }
}
/// <summary>
/// Gets or sets the http headers.
/// </summary>
public virtual WebHeaderCollection Headers
{
get { return _httpWebRequest.Headers; }
set { _httpWebRequest.Headers = value; }
}
#if !(NETFX_CORE || HTTPHELPER_PORTABLE_LIBRARY)
/// <summary>
/// Gets or sets a value that indicates whether the request should follow redirection responses.
/// </summary>
public virtual bool AllowAutoRedirect
{
get { return _httpWebRequest.AllowAutoRedirect; }
set { _httpWebRequest.AllowAutoRedirect = value; }
}
#endif
#if !(WINDOWS_PHONE || NETFX_CORE || HTTPHELPER_PORTABLE_LIBRARY)
/// <summary>
/// Gets or sets the content length.
/// </summary>
public virtual long ContentLength
{
get { return _httpWebRequest.ContentLength; }
set { _httpWebRequest.ContentLength = value; }
}
/// <summary>
/// True to enable buffering of the data sent to the Internet resource; false to disable buffering. The default is true.
/// </summary>
public virtual bool AllowWriteStreamBuffering
{
get { return _httpWebRequest.AllowWriteStreamBuffering; }
set { _httpWebRequest.AllowWriteStreamBuffering = value; }
}
#endif
#if !(NETFX_CORE || HTTPHELPER_PORTABLE_LIBRARY)
/// <summary>
/// Gets or sets the user agent.
/// </summary>
public virtual string UserAgent
{
get { return _httpWebRequest.UserAgent; }
set { _httpWebRequest.UserAgent = value; }
}
#endif
/// <summary>
/// Gets or sets the cookie container.
/// </summary>
public virtual CookieContainer CookieContainer
{
get { return _httpWebRequest.CookieContainer; }
set { _httpWebRequest.CookieContainer = value; }
}
/// <summary>
/// Gets or sets the credentials.
/// </summary>
public virtual ICredentials Credentials
{
get { return _httpWebRequest.Credentials; }
set { _httpWebRequest.Credentials = value; }
}
/// <summary>
/// Gets the request uri.
/// </summary>
public virtual Uri RequestUri
{
get { return _httpWebRequest.RequestUri; }
}
/// <summary>
/// Gets or sets the accept.
/// </summary>
public virtual string Accept
{
get { return _httpWebRequest.Accept; }
set { _httpWebRequest.Accept = value; }
}
#if !SILVERLIGHT
#if !NETFX_CORE
#if !HTTPHELPER_PORTABLE_LIBRARY
/// <summary>
/// Gets or sets the proxy.
/// </summary>
public virtual IWebProxy Proxy
{
get { return _httpWebRequest.Proxy; }
set { _httpWebRequest.Proxy = value; }
}
/// <summary>
/// Gets the service point to use for the request.
/// </summary>
public virtual ServicePoint ServicePoint
{
get { return _httpWebRequest.ServicePoint; }
}
/// <summary>
/// Gets or sets the decompression method.
/// </summary>
public virtual DecompressionMethods AutomaticDecompression
{
get { return _httpWebRequest.AutomaticDecompression; }
set { _httpWebRequest.AutomaticDecompression = value; }
}
/// <summary>
/// Gets or sets the connection.
/// </summary>
public virtual string Connection
{
get { return _httpWebRequest.Connection; }
set { _httpWebRequest.Connection = value; }
}
/// <summary>
/// Gets or sets the expect.
/// </summary>
public virtual string Expect
{
get { return _httpWebRequest.Expect; }
set { _httpWebRequest.Expect = value; }
}
/// <summary>
/// Gets or sets the if modified since.
/// </summary>
public virtual DateTime IfModifiedSince
{
get { return _httpWebRequest.IfModifiedSince; }
set { _httpWebRequest.IfModifiedSince = value; }
}
/// <summary>
/// Gets or sets the read write timeout.
/// </summary>
public virtual int ReadWriteTimeout
{
get { return _httpWebRequest.ReadWriteTimeout; }
set { _httpWebRequest.ReadWriteTimeout = value; }
}
/// <summary>
/// Gets or sets the referrer.
/// </summary>
public virtual string Referer
{
get { return _httpWebRequest.Referer; }
set { _httpWebRequest.Referer = value; }
}
/// <summary>
/// Gets or set the time in milliseconds, before the request times out.
/// </summary>
public virtual int Timeout
{
get { return _httpWebRequest.Timeout; }
set { _httpWebRequest.Timeout = value; }
}
/// <summary>
/// Gets or sets the transfer encoding.
/// </summary>
public virtual string TransferEncoding
{
get { return _httpWebRequest.TransferEncoding; }
set { _httpWebRequest.TransferEncoding = value; }
}
#endif
#endif
#endif
/// <summary>
/// Try to set the value of the content length header.
/// </summary>
/// <param name="contentLength">
/// The Content-Length header value.
/// </param>
/// <returns>
/// True if ContentLength set, otherwise false.
/// </returns>
public virtual bool TrySetContentLength(long contentLength)
{
#if !(WINDOWS_PHONE || NETFX_CORE || HTTPHELPER_PORTABLE_LIBRARY)
ContentLength = contentLength;
return true;
#else
return false;
#endif
}
/// <summary>
/// Try to set the value of the user agenet header.
/// </summary>
/// <param name="userAgent">
/// The UserAgent value.
/// </param>
/// <returns>
/// True if UserAgent is set, otherwise false.
/// </returns>
public virtual bool TrySetUserAgent(string userAgent)
{
#if (!(SILVERLIGHT || NETFX_CORE || HTTPHELPER_PORTABLE_LIBRARY) || WINDOWS_PHONE)
UserAgent = userAgent;
return true;
#else
return false;
#endif
}
/// <summary>
/// Begins the get response.
/// </summary>
/// <param name="callback">
/// The callback.
/// </param>
/// <param name="state">
/// The state.
/// </param>
/// <returns>
/// The async result.
/// </returns>
public virtual IAsyncResult BeginGetResponse(AsyncCallback callback, object state)
{
return _httpWebRequest.BeginGetResponse(callback, state);
}
/// <summary>
/// Begins getting the request stream.
/// </summary>
/// <param name="callback">
/// The callback.
/// </param>
/// <param name="state">
/// The state.
/// </param>
/// <returns>
/// The async result.
/// </returns>
public virtual IAsyncResult BeginGetRequestStream(AsyncCallback callback, object state)
{
return _httpWebRequest.BeginGetRequestStream(callback, state);
}
/// <summary>
/// Ends the http web get response.
/// </summary>
/// <param name="asyncResult">
/// The async result.
/// </param>
/// <returns>
/// The http web response.
/// </returns>
public virtual HttpWebResponseWrapper EndGetResponse(IAsyncResult asyncResult)
{
var httpWebResponse = (HttpWebResponse)_httpWebRequest.EndGetResponse(asyncResult);
return new HttpWebResponseWrapper(httpWebResponse);
}
/// <summary>
/// Ends the get request stream.
/// </summary>
/// <param name="asyncResult">
/// The async result.
/// </param>
/// <returns>
/// The request stream.
/// </returns>
public virtual Stream EndGetRequestStream(IAsyncResult asyncResult)
{
return _httpWebRequest.EndGetRequestStream(asyncResult);
}
#if HTTPHELPER_TPL
/// <summary>
/// Asynchronously gets the response.
/// </summary>
/// <returns></returns>
public virtual Task<HttpWebResponseWrapper> GetResponseAsync()
{
return Task<HttpWebResponseWrapper>.Factory.FromAsync(BeginGetResponse, EndGetResponse, null);
}
/// <summary>
/// Asynchronously gets the request stream.
/// </summary>
/// <returns></returns>
public virtual Task<Stream> GetRequestStreamAsync()
{
return Task<Stream>.Factory.FromAsync(BeginGetRequestStream, EndGetRequestStream, null);
}
#endif
#if !(SILVERLIGHT || NETFX_CORE || HTTPHELPER_PORTABLE_LIBRARY)
/// <summary>
/// Gets the response.
/// </summary>
/// <returns></returns>
public virtual HttpWebResponseWrapper GetResponse()
{
return new HttpWebResponseWrapper((HttpWebResponse)_httpWebRequest.GetResponse());
}
/// <summary>
/// Gets the request stream.
/// </summary>
/// <returns></returns>
public virtual Stream GetRequestStream()
{
return _httpWebRequest.GetRequestStream();
}
#endif
private object _cancelledLock = new object();
/// <summary>
/// Aborts the http web request.
/// </summary>
public virtual void Abort()
{
lock (_cancelledLock)
{
try
{
_httpWebRequest.Abort();
}
catch (WebException ex)
{
throw new WebExceptionWrapper(ex);
}
finally
{
IsCancelled = true;
}
}
}
/// <summary>
/// Gets or sets if the request to an Internet resource was cancelled.
/// </summary>
public virtual bool IsCancelled { get; set; }
}
/// <summary>
/// Wrapper for http web response.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public class HttpWebResponseWrapper
{
/// <summary>
/// The http web response.
/// </summary>
private readonly HttpWebResponse _httpWebResponse;
/// <summary>
/// Initializes a new instance of the <see cref="HttpWebResponseWrapper"/> class.
/// </summary>
protected HttpWebResponseWrapper()
{
}
/// <summary>
/// Initializes a new instance of the <see cref="HttpWebResponseWrapper"/> class.
/// </summary>
/// <param name="httpWebResponse">
/// The http web response.
/// </param>
public HttpWebResponseWrapper(HttpWebResponse httpWebResponse)
{
if (httpWebResponse == null)
throw new ArgumentNullException("httpWebResponse");
_httpWebResponse = httpWebResponse;
}
/// <summary>
/// Gets the http method.
/// </summary>
public virtual string Method
{
get { return _httpWebResponse.Method; }
}
/// <summary>
/// Gets the content length.
/// </summary>
public virtual long ContentLength
{
get { return _httpWebResponse.ContentLength; }
}
/// <summary>
/// Gets the content type.
/// </summary>
public virtual string ContentType
{
get { return _httpWebResponse.ContentType; }
}
/// <summary>
/// Gets the response uri.
/// </summary>
public virtual Uri ResponseUri
{
get { return _httpWebResponse.ResponseUri; }
}
/// <summary>
/// Gets the http status code.
/// </summary>
public virtual HttpStatusCode StatusCode
{
get { return _httpWebResponse.StatusCode; }
}
/// <summary>
/// Gets the status description.
/// </summary>
public virtual string StatusDescription
{
get { return _httpWebResponse.StatusDescription; }
}
/// <summary>
/// Gets the http cookies.
/// </summary>
public virtual CookieCollection Cookies
{
get { return _httpWebResponse.Cookies; }
}
/// <summary>
/// Gets the http headers.
/// </summary>
public virtual WebHeaderCollection Headers
{
get { return _httpWebResponse.Headers; }
}
#if !SILVERLIGHT
#if !(NETFX_CORE || HTTPHELPER_PORTABLE_LIBRARY)
/// <summary>
/// Gets the content encoding.
/// </summary>
public virtual string ContentEncoding
{
get { return _httpWebResponse.ContentEncoding; }
}
/// <summary>
/// Gets the character set.
/// </summary>
public virtual string CharacterSet
{
get { return _httpWebResponse.CharacterSet; }
}
/// <summary>
/// Gets a value indicating whether response is mutually authenticated.
/// </summary>
public virtual bool IsMutuallyAuthenticated
{
get { return _httpWebResponse.IsMutuallyAuthenticated; }
}
/// <summary>
/// Gets the last modified date and time.
/// </summary>
public virtual DateTime LastModified
{
get { return _httpWebResponse.LastModified; }
}
/// <summary>
/// Gets the protocol version.
/// </summary>
public virtual Version ProtocolVersion
{
get { return _httpWebResponse.ProtocolVersion; }
}
/// <summary>
/// Gets the server.
/// </summary>
public virtual string Server
{
get { return _httpWebResponse.Server; }
}
#endif
#endif
/// <summary>
/// Gets the response stream.
/// </summary>
/// <returns>
/// The response stream.
/// </returns>
public virtual Stream GetResponseStream()
{
return _httpWebResponse.GetResponseStream();
}
}
/// <summary>
/// Wrapper for web exceptions.
/// </summary>
#if !(NETFX_CORE || SILVERLIGHT || HTTPHELPER_PORTABLE_LIBRARY)
[Serializable]
#endif
[EditorBrowsable(EditorBrowsableState.Never)]
public class WebExceptionWrapper : Exception
{
private readonly WebException _webException;
private readonly WebExceptionStatus _status = WebExceptionStatus.UnknownError;
/// <summary>
/// Initializes a new instance of the <see cref="WebExceptionWrapper"/> class.
/// </summary>
protected WebExceptionWrapper() { }
/// <summary>
/// Initializes a new instance of the <see cref="WebExceptionWrapper"/> class.
/// </summary>
/// <param name="message">Error message.</param>
/// <param name="innerException">The inner exception.</param>
protected WebExceptionWrapper(string message, Exception innerException)
: base(message, innerException)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="WebExceptionWrapper"/> class.
/// </summary>
public WebExceptionWrapper(WebException webException)
: base(webException == null ? null : webException.Message, webException == null ? null : webException.InnerException)
{
_webException = webException;
_status = webException == null ? WebExceptionStatus.UnknownError : webException.Status;
}
#if !(NETFX_CORE || SILVERLIGHT || HTTPHELPER_PORTABLE_LIBRARY)
/// <summary>
/// Initializes a new instance of the <see cref="WebExceptionWrapper"/> class.
/// </summary>
/// <param name="info">The <see cref="T:System.Runtime.Serialization.SerializationInfo"/> that holds the serialized object data about the exception being thrown.</param>
/// <param name="context">The <see cref="T:System.Runtime.Serialization.StreamingContext"/> that contains contextual information about the source or destination.</param>
/// <exception cref="T:System.ArgumentNullException">The <paramref name="info"/> parameter is null. </exception>
/// <exception cref="T:System.Runtime.Serialization.SerializationException">The class name is null or <see cref="P:System.Exception.HResult"/> is zero (0). </exception>
protected WebExceptionWrapper(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context)
: base(info, context)
{
}
#endif
/// <summary>
/// Gets the response of the web exception.
/// </summary>
/// <returns>
/// Returns the response of the web exception if it has a response, otherwise null.
/// </returns>
public virtual HttpWebResponseWrapper GetResponse()
{
return _webException.Response == null
? null
: new HttpWebResponseWrapper((HttpWebResponse)_webException.Response);
}
/// <summary>
/// Returns the actual web exception.
/// </summary>
public virtual WebException ActualWebException
{
get { return _webException; }
}
/// <summary>
/// Returns the web exception status.
/// </summary>
public virtual WebExceptionStatus Status
{
get { return _status; }
}
}
}
namespace $rootnamespace$
{
/// <summary>
/// Event handler for open read complete.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
#if HTTPHELPER_PUBLIC
public
#else
internal
#endif
delegate void OpenReadCompletedEventHandler(object sender, OpenReadCompletedEventArgs e);
/// <summary>
/// Event handler for open write complete.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
#if HTTPHELPER_PUBLIC
public
#else
internal
#endif
delegate void OpenWriteCompletedEventHandler(object sender, OpenWriteCompletedEventArgs e);
/// <summary>
/// Open read completed event args.
/// </summary>
#if HTTPHELPER_PUBLIC
public
#else
internal
#endif
class OpenReadCompletedEventArgs : AsyncCompletedEventArgs
{
private readonly Stream _result;
/// <summary>
/// Initializes a new instance of the <see cref="OpenReadCompletedEventArgs"/> class.
/// </summary>
/// <param name="result">The response stream.</param>
/// <param name="error">The exception.</param>
/// <param name="cancelled"></param>
/// <param name="userState"></param>
public OpenReadCompletedEventArgs(Stream result, Exception error, bool cancelled, object userState)
: base(error, cancelled, userState)
{
_result = result;
}
/// <summary>
/// The response stream.
/// </summary>
public Stream Result
{
get
{
RaiseExceptionIfNecessary();
return _result;
}
}
}
/// <summary>
/// Open write completed event args.
/// </summary>
#if HTTPHELPER_PUBLIC
public
#else
internal
#endif
class OpenWriteCompletedEventArgs : AsyncCompletedEventArgs
{
private readonly Stream _result;
/// <summary>
/// Initializes a new instance of the <see cref="OpenWriteCompletedEventArgs"/> class.
/// </summary>
/// <param name="result">The request stream.</param>
/// <param name="error"></param>
/// <param name="cancelled"></param>
/// <param name="userState"></param>
public OpenWriteCompletedEventArgs(Stream result, Exception error, bool cancelled, object userState)
: base(error, cancelled, userState)
{
_result = result;
}
/// <summary>
/// The request stream.
/// </summary>
public Stream Result
{
get
{
RaiseExceptionIfNecessary();
return _result;
}
}
}
/// <summary>
/// Http helper.
/// </summary>
#if HTTPHELPER_PUBLIC
public
#else
internal
#endif
class HttpHelper
{
private const string ErrorPerformingHttpRequest = "An error occurred performing a http web request.";
/// <summary>
/// application/form-url-encoded content type
/// </summary>
public const string ApplicationFormUrlEncodedContentType = "application/form-url-encoded";
/// <summary>
/// application/x-www-form-urlencoded content type
/// </summary>
public const string ApplicationXWWWFormUrlEncodedContentType = "application/x-www-form-urlencoded";
/// <summary>
/// Returns multipart/form-data; boundary=.....
/// </summary>
/// <param name="boundary">The multipart boundary</param>
/// <returns>
/// multipart/form-data; boundary=... content type
/// </returns>
public static string GetMultipartFormDataBoundaryContentType(string boundary)
{
return string.Concat("multipart/form-data; boundary=", boundary);
}
/// <summary>
/// Gets the inner exception.
/// </summary>
public Exception InnerException
{
get { return _innerException; }
}
/// <summary>
/// Gets or sets the timeout.
/// </summary>
public int Timeout { get; set; }
/// <summary>
/// Open read completed event handler.
/// </summary>
public event OpenReadCompletedEventHandler OpenReadCompleted;
/// <summary>
/// Open write completed event handler.
/// </summary>
public event OpenWriteCompletedEventHandler OpenWriteCompleted;
private readonly HttpWebRequestWrapper _httpWebRequest;
private HttpWebResponseWrapper _httpWebResponse;
private Exception _innerException;
/// <summary>
/// Initializes a new instance of the <see cref="HttpHelper"/> class.
/// </summary>
/// <param name="url"></param>
public HttpHelper(Uri url)
: this(new HttpWebRequestWrapper((HttpWebRequest)WebRequest.Create(url)))
{
}
/// <summary>
/// Initializes a new instance of the <see cref="HttpHelper"/> class.
/// </summary>
/// <param name="url"></param>
public HttpHelper(string url)
: this(new Uri(url))
{
}
/// <summary>
/// Initializes a new instance of the <see cref="HttpHelper"/> class.
/// </summary>
/// <param name="httpWebRequest"></param>
public HttpHelper(HttpWebRequestWrapper httpWebRequest)
{
_httpWebRequest = httpWebRequest;
}
/// <summary>
/// Gets the http web request.
/// </summary>
public HttpWebRequestWrapper HttpWebRequest
{
get { return _httpWebRequest; }
}
/// <summary>
/// Gets the http web response.
/// </summary>
public HttpWebResponseWrapper HttpWebResponse
{
get { return _httpWebResponse; }
}
#if !(SILVERLIGHT || NETFX_CORE || HTTPHELPER_PORTABLE_LIBRARY)
/// <summary>
/// Opens the request stream synchronously for write.
/// </summary>
/// <returns></returns>
/// <exception cref="WebExceptionWrapper"></exception>
/// <exception cref="Exception"></exception>
public virtual Stream OpenWrite()
{
try
{
return _httpWebRequest.GetRequestStream();
}
catch (WebException webException)
{
if (webException.Response != null)
_httpWebResponse = new HttpWebResponseWrapper((HttpWebResponse)webException.Response);
_innerException = webException;
throw new WebExceptionWrapper(webException);
}
catch (WebExceptionWrapper webException)
{
_httpWebResponse = webException.GetResponse();
_innerException = webException;
throw;
}
catch (Exception ex)
{
_innerException = new WebExceptionWrapper(new WebException(ErrorPerformingHttpRequest, ex));
throw _innerException;
}
}
/// <summary>
/// Opens the response stream synchronously for read.
/// </summary>
/// <returns></returns>
/// <exception cref="Exception"></exception>
public virtual Stream OpenRead()
{
try
{
if (_httpWebResponse == null)
_httpWebResponse = _httpWebRequest.GetResponse();
return _httpWebResponse.GetResponseStream();
}
catch (WebException webException)
{
_innerException = new WebExceptionWrapper(webException);
if (webException.Response != null)
{
_httpWebResponse = new HttpWebResponseWrapper((HttpWebResponse) webException.Response);
var stream = _httpWebResponse.GetResponseStream();
if (stream != null)
return stream;
}
throw _innerException;
}
catch (WebExceptionWrapper webException)
{
_innerException = webException;
_httpWebResponse = webException.GetResponse();
if(_httpWebResponse!=null)
{
var stream = _httpWebResponse.GetResponseStream();
if (stream != null)
return stream;
}
throw _innerException;
}
catch (Exception ex)
{
_innerException = new WebExceptionWrapper(new WebException(ErrorPerformingHttpRequest, ex));
throw _innerException;
}
}
#endif
/// <summary>
/// Asynchronously opens the response stream for read.
/// </summary>
/// <param name="userToken"></param>
public virtual void OpenReadAsync(object userToken)
{
if (_httpWebResponse == null)