@@ -38,21 +38,27 @@ public static class APIConsumer
38
38
/// <returns>String Api result</returns>
39
39
public static string POSTMethod ( string url , string JsonBody , string Authorization = "" )
40
40
{
41
- string ContentResult = string . Empty ;
41
+ string ContentResult = string . Empty ;
42
42
try
43
43
{
44
- SetSSL ( ) ;
44
+ ServicePointManager . Expect100Continue = true ;
45
+ ServicePointManager . SecurityProtocol = SecurityProtocolType . Tls
46
+ | SecurityProtocolType . Ssl3 ;
47
+
45
48
HttpWebRequest request = ( HttpWebRequest ) WebRequest . Create ( url ) ;
46
49
request . ContentType = CONTENTTYPE ;
47
- request . Method = POST_WebMethod ;
50
+ request . Method = POST_WebMethod ;
48
51
49
52
if ( ! String . IsNullOrEmpty ( Authorization ) )
50
53
request . Headers . Add ( HttpRequestHeader . Authorization , Authorization ) ;
51
54
52
- using ( var streamWriter = new StreamWriter ( request . GetRequestStream ( ) ) )
55
+ if ( ! String . IsNullOrEmpty ( JsonBody ) )
53
56
{
54
- streamWriter . Write ( JsonBody ) ;
55
- streamWriter . Flush ( ) ;
57
+ using ( var streamWriter = new StreamWriter ( request . GetRequestStream ( ) ) )
58
+ {
59
+ streamWriter . Write ( JsonBody ) ;
60
+ streamWriter . Flush ( ) ;
61
+ }
56
62
}
57
63
58
64
var httpResponse = ( HttpWebResponse ) request . GetResponse ( ) ;
@@ -80,6 +86,68 @@ public static string POSTMethod(string url, string JsonBody, string Authorizatio
80
86
return ContentResult ;
81
87
}
82
88
89
+
90
+ /// <summary>
91
+ /// POST to Resful API sending Json body.
92
+ /// </summary>
93
+ /// <param name="url">API URL</param>
94
+ /// <param name="JsonBody">Content Application By Default Json</param>
95
+ /// <returns>String Api result</returns>
96
+ public static string POSTMethod ( string url , string JsonBody )
97
+ {
98
+ string ContentResult = string . Empty ;
99
+ try
100
+ {
101
+ SetSSL ( ) ;
102
+ HttpWebRequest request = ( HttpWebRequest ) WebRequest . Create ( url ) ;
103
+ request . ContentType = CONTENTTYPE ;
104
+ request . Method = POST_WebMethod ;
105
+
106
+ if ( ! String . IsNullOrEmpty ( JsonBody ) )
107
+ {
108
+ using ( var streamWriter = new StreamWriter ( request . GetRequestStream ( ) ) )
109
+ {
110
+ streamWriter . Write ( JsonBody ) ;
111
+ streamWriter . Flush ( ) ;
112
+ }
113
+ }
114
+
115
+ var httpResponse = ( HttpWebResponse ) request . GetResponse ( ) ;
116
+ using ( var streamReader = new StreamReader ( httpResponse . GetResponseStream ( ) ) )
117
+ {
118
+ var result = streamReader ? . ReadToEnd ( ) ;
119
+ ContentResult = result ;
120
+ }
121
+ }
122
+ catch ( WebException ex )
123
+ {
124
+ using ( var stream = ex . Response ? . GetResponseStream ( ) )
125
+ {
126
+ if ( stream != null )
127
+ {
128
+ using ( var reader = new StreamReader ( stream ) )
129
+ {
130
+ var result = reader . ReadToEnd ( ) ;
131
+ ContentResult = result ;
132
+ }
133
+ }
134
+ else
135
+ {
136
+ ContentResult = ex . Message . ToString ( ) ;
137
+ }
138
+
139
+ }
140
+
141
+ }
142
+ catch ( Exception ex )
143
+ {
144
+ ContentResult = ex . Message . ToString ( ) ;
145
+ throw ex ;
146
+ }
147
+
148
+ return ContentResult ;
149
+ }
150
+
83
151
/// <summary>
84
152
/// POST to Resful API sending Json body.
85
153
/// </summary>
@@ -125,11 +193,20 @@ public static string POSTMethod_Header(string url, string JsonBody = "", string
125
193
}
126
194
catch ( WebException ex )
127
195
{
128
- using ( var stream = ex . Response . GetResponseStream ( ) )
129
- using ( var reader = new StreamReader ( stream ) )
196
+ using ( var stream = ex . Response ? . GetResponseStream ( ) )
130
197
{
131
- var result = reader . ReadToEnd ( ) ;
132
- ContentResult = result ;
198
+ if ( stream != null )
199
+ {
200
+ using ( var reader = new StreamReader ( stream ) )
201
+ {
202
+ var result = reader . ReadToEnd ( ) ;
203
+ ContentResult = result ;
204
+ }
205
+ }
206
+ else
207
+ {
208
+ ContentResult = ex . Message . ToString ( ) ;
209
+ }
133
210
}
134
211
}
135
212
catch ( Exception ex )
@@ -169,11 +246,20 @@ public static string GETMethod(string url, string Authorization = "")
169
246
}
170
247
catch ( WebException ex )
171
248
{
172
- using ( var stream = ex . Response . GetResponseStream ( ) )
173
- using ( var reader = new StreamReader ( stream ) )
249
+ using ( var stream = ex . Response ? . GetResponseStream ( ) )
174
250
{
175
- var result = reader . ReadToEnd ( ) ;
176
- ContentResult = result ;
251
+ if ( stream != null )
252
+ {
253
+ using ( var reader = new StreamReader ( stream ) )
254
+ {
255
+ var result = reader . ReadToEnd ( ) ;
256
+ ContentResult = result ;
257
+ }
258
+ }
259
+ else
260
+ {
261
+ ContentResult = ex . Message . ToString ( ) ;
262
+ }
177
263
}
178
264
}
179
265
catch ( Exception ex )
@@ -221,11 +307,20 @@ public static string GETMethod_Headers(string url, string Headers)
221
307
}
222
308
catch ( WebException ex )
223
309
{
224
- using ( var stream = ex . Response . GetResponseStream ( ) )
225
- using ( var reader = new StreamReader ( stream ) )
310
+ using ( var stream = ex . Response ? . GetResponseStream ( ) )
226
311
{
227
- var result = reader . ReadToEnd ( ) ;
228
- ContentResult = result ;
312
+ if ( stream != null )
313
+ {
314
+ using ( var reader = new StreamReader ( stream ) )
315
+ {
316
+ var result = reader . ReadToEnd ( ) ;
317
+ ContentResult = result ;
318
+ }
319
+ }
320
+ else
321
+ {
322
+ ContentResult = ex . Message . ToString ( ) ;
323
+ }
229
324
}
230
325
}
231
326
catch ( Exception ex )
@@ -279,11 +374,20 @@ public static string GETMethod_Headers(string url, string JsonBody = "", string
279
374
}
280
375
catch ( WebException ex )
281
376
{
282
- using ( var stream = ex . Response . GetResponseStream ( ) )
283
- using ( var reader = new StreamReader ( stream ) )
377
+ using ( var stream = ex . Response ? . GetResponseStream ( ) )
284
378
{
285
- var result = reader . ReadToEnd ( ) ;
286
- ContentResult = result ;
379
+ if ( stream != null )
380
+ {
381
+ using ( var reader = new StreamReader ( stream ) )
382
+ {
383
+ var result = reader . ReadToEnd ( ) ;
384
+ ContentResult = result ;
385
+ }
386
+ }
387
+ else
388
+ {
389
+ ContentResult = ex . Message . ToString ( ) ;
390
+ }
287
391
}
288
392
}
289
393
catch ( Exception ex )
@@ -324,11 +428,20 @@ public static string GETMethod(string url, string Id, string Authorization = "")
324
428
}
325
429
catch ( WebException ex )
326
430
{
327
- using ( var stream = ex . Response . GetResponseStream ( ) )
328
- using ( var reader = new StreamReader ( stream ) )
431
+ using ( var stream = ex . Response ? . GetResponseStream ( ) )
329
432
{
330
- var result = reader . ReadToEnd ( ) ;
331
- ContentResult = result ;
433
+ if ( stream != null )
434
+ {
435
+ using ( var reader = new StreamReader ( stream ) )
436
+ {
437
+ var result = reader . ReadToEnd ( ) ;
438
+ ContentResult = result ;
439
+ }
440
+ }
441
+ else
442
+ {
443
+ ContentResult = ex . Message . ToString ( ) ;
444
+ }
332
445
}
333
446
}
334
447
catch ( Exception ex )
@@ -370,11 +483,20 @@ public static string GETMethod<T>(string url, ref T ObjectResult, string Authori
370
483
}
371
484
catch ( WebException ex )
372
485
{
373
- using ( var stream = ex . Response . GetResponseStream ( ) )
374
- using ( var reader = new StreamReader ( stream ) )
486
+ using ( var stream = ex . Response ? . GetResponseStream ( ) )
375
487
{
376
- var result = reader . ReadToEnd ( ) ;
377
- ContentResult = result ;
488
+ if ( stream != null )
489
+ {
490
+ using ( var reader = new StreamReader ( stream ) )
491
+ {
492
+ var result = reader . ReadToEnd ( ) ;
493
+ ContentResult = result ;
494
+ }
495
+ }
496
+ else
497
+ {
498
+ ContentResult = ex . Message . ToString ( ) ;
499
+ }
378
500
}
379
501
}
380
502
catch ( Exception ex )
@@ -416,11 +538,20 @@ public static string GETMethod<T>(string url, string Id, ref T ObjectResult, str
416
538
}
417
539
catch ( WebException ex )
418
540
{
419
- using ( var stream = ex . Response . GetResponseStream ( ) )
420
- using ( var reader = new StreamReader ( stream ) )
541
+ using ( var stream = ex . Response ? . GetResponseStream ( ) )
421
542
{
422
- var result = reader . ReadToEnd ( ) ;
423
- ContentResult = result ;
543
+ if ( stream != null )
544
+ {
545
+ using ( var reader = new StreamReader ( stream ) )
546
+ {
547
+ var result = reader . ReadToEnd ( ) ;
548
+ ContentResult = result ;
549
+ }
550
+ }
551
+ else
552
+ {
553
+ ContentResult = ex . Message . ToString ( ) ;
554
+ }
424
555
}
425
556
}
426
557
catch ( Exception ex )
@@ -434,9 +565,10 @@ public static string GETMethod<T>(string url, string Id, ref T ObjectResult, str
434
565
435
566
private static void SetSSL ( )
436
567
{
437
- ServicePointManager . Expect100Continue = true ;
438
- ServicePointManager . SecurityProtocol = SecurityProtocolType . Tls
439
- | SecurityProtocolType . Ssl3 ;
568
+ System . Net . ServicePointManager . ServerCertificateValidationCallback = ( senderX , certificate , chain , sslPolicyErrors ) => { return true ; } ;
569
+ //ServicePointManager.Expect100Continue = true;
570
+ //ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls
571
+ // | SecurityProtocolType.Ssl3;
440
572
}
441
573
}
442
574
}
0 commit comments