Skip to content

Commit c09955b

Browse files
author
gdiaz
committed
Fix bugs #16 & #15.
Added SetSSL method.
1 parent 28af78c commit c09955b

File tree

14 files changed

+178
-46
lines changed

14 files changed

+178
-46
lines changed

.vs/SQL-APIConsumer/v15/.suo

11.5 KB
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

API_Consumer/Consumers/APIConsumer.cs

Lines changed: 169 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -38,21 +38,27 @@ public static class APIConsumer
3838
/// <returns>String Api result</returns>
3939
public static string POSTMethod(string url, string JsonBody, string Authorization = "")
4040
{
41-
string ContentResult = string.Empty ;
41+
string ContentResult = string.Empty;
4242
try
4343
{
44-
SetSSL();
44+
ServicePointManager.Expect100Continue = true;
45+
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls
46+
| SecurityProtocolType.Ssl3;
47+
4548
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
4649
request.ContentType = CONTENTTYPE;
47-
request.Method = POST_WebMethod;
50+
request.Method = POST_WebMethod;
4851

4952
if (!String.IsNullOrEmpty(Authorization))
5053
request.Headers.Add(HttpRequestHeader.Authorization, Authorization);
5154

52-
using (var streamWriter = new StreamWriter(request.GetRequestStream()))
55+
if (!String.IsNullOrEmpty(JsonBody))
5356
{
54-
streamWriter.Write(JsonBody);
55-
streamWriter.Flush();
57+
using (var streamWriter = new StreamWriter(request.GetRequestStream()))
58+
{
59+
streamWriter.Write(JsonBody);
60+
streamWriter.Flush();
61+
}
5662
}
5763

5864
var httpResponse = (HttpWebResponse)request.GetResponse();
@@ -80,6 +86,68 @@ public static string POSTMethod(string url, string JsonBody, string Authorizatio
8086
return ContentResult;
8187
}
8288

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+
83151
/// <summary>
84152
/// POST to Resful API sending Json body.
85153
/// </summary>
@@ -125,11 +193,20 @@ public static string POSTMethod_Header(string url, string JsonBody = "", string
125193
}
126194
catch (WebException ex)
127195
{
128-
using (var stream = ex.Response.GetResponseStream())
129-
using (var reader = new StreamReader(stream))
196+
using (var stream = ex.Response?.GetResponseStream())
130197
{
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+
}
133210
}
134211
}
135212
catch (Exception ex)
@@ -169,11 +246,20 @@ public static string GETMethod(string url, string Authorization = "")
169246
}
170247
catch (WebException ex)
171248
{
172-
using (var stream = ex.Response.GetResponseStream())
173-
using (var reader = new StreamReader(stream))
249+
using (var stream = ex.Response?.GetResponseStream())
174250
{
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+
}
177263
}
178264
}
179265
catch (Exception ex)
@@ -221,11 +307,20 @@ public static string GETMethod_Headers(string url, string Headers)
221307
}
222308
catch (WebException ex)
223309
{
224-
using (var stream = ex.Response.GetResponseStream())
225-
using (var reader = new StreamReader(stream))
310+
using (var stream = ex.Response?.GetResponseStream())
226311
{
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+
}
229324
}
230325
}
231326
catch (Exception ex)
@@ -279,11 +374,20 @@ public static string GETMethod_Headers(string url, string JsonBody = "", string
279374
}
280375
catch (WebException ex)
281376
{
282-
using (var stream = ex.Response.GetResponseStream())
283-
using (var reader = new StreamReader(stream))
377+
using (var stream = ex.Response?.GetResponseStream())
284378
{
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+
}
287391
}
288392
}
289393
catch (Exception ex)
@@ -324,11 +428,20 @@ public static string GETMethod(string url, string Id, string Authorization = "")
324428
}
325429
catch (WebException ex)
326430
{
327-
using (var stream = ex.Response.GetResponseStream())
328-
using (var reader = new StreamReader(stream))
431+
using (var stream = ex.Response?.GetResponseStream())
329432
{
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+
}
332445
}
333446
}
334447
catch (Exception ex)
@@ -370,11 +483,20 @@ public static string GETMethod<T>(string url, ref T ObjectResult, string Authori
370483
}
371484
catch (WebException ex)
372485
{
373-
using (var stream = ex.Response.GetResponseStream())
374-
using (var reader = new StreamReader(stream))
486+
using (var stream = ex.Response?.GetResponseStream())
375487
{
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+
}
378500
}
379501
}
380502
catch (Exception ex)
@@ -416,11 +538,20 @@ public static string GETMethod<T>(string url, string Id, ref T ObjectResult, str
416538
}
417539
catch (WebException ex)
418540
{
419-
using (var stream = ex.Response.GetResponseStream())
420-
using (var reader = new StreamReader(stream))
541+
using (var stream = ex.Response?.GetResponseStream())
421542
{
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+
}
424555
}
425556
}
426557
catch (Exception ex)
@@ -434,9 +565,10 @@ public static string GETMethod<T>(string url, string Id, ref T ObjectResult, str
434565

435566
private static void SetSSL()
436567
{
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;
440572
}
441573
}
442574
}

API_Consumer/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,5 @@
1919
// Build Number
2020
// Revision
2121
//
22-
[assembly: AssemblyVersion("2.0.0.0")]
23-
[assembly: AssemblyFileVersion("2.0.0.0")]
22+
[assembly: AssemblyVersion("2.0.0.1")]
23+
[assembly: AssemblyFileVersion("2.0.0.1")]
1 KB
Binary file not shown.
2 KB
Binary file not shown.
1.71 KB
Binary file not shown.

API_Consumer/obj/Debug/API_CONSUMER.generated.sql

Lines changed: 2 additions & 2 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)