-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRestHelper.cs
More file actions
117 lines (104 loc) · 4.6 KB
/
RestHelper.cs
File metadata and controls
117 lines (104 loc) · 4.6 KB
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
using System;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Security.Principal;
using System.Web;
using System.Web.Script.Serialization;
namespace Common.Helpers
{
public class RestHelper
{
public T Post<T, M>(M parameterModel, string url)
{
T responseData;
var wi = (WindowsIdentity)HttpContext.Current.User.Identity;
var wic = wi.Impersonate();
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.UseDefaultCredentials = true;
request.Method = HttpMethod.Post.ToString();
request.ContentType = "application/json; charset=utf-8";
JavaScriptSerializer jScriptSerializer = new JavaScriptSerializer();
string json = jScriptSerializer.Serialize(parameterModel);
StreamWriter writerObject = new StreamWriter(request.GetRequestStream());
writerObject.Write(json); writerObject.Close();
string response = GetResponse(request, wic);
// deserialize the response content
responseData = jScriptSerializer.Deserialize<T>(response);
return responseData;
}
public T Put<T, M>(M parameterModel, string url)
{
var wi = (WindowsIdentity)HttpContext.Current.User.Identity;
var wic = wi.Impersonate();
T responseData;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.UseDefaultCredentials = true;
request.Method = HttpMethod.Put.ToString();
request.ContentType = "application/json; charset=utf-8";
JavaScriptSerializer jScriptSerializer = new JavaScriptSerializer();
string json = jScriptSerializer.Serialize(parameterModel);
StreamWriter writerObject = new StreamWriter(request.GetRequestStream());
writerObject.Write(json);
writerObject.Close();
string response = GetResponse(request, wic);
// deserialize the response content
responseData = jScriptSerializer.Deserialize<T>(response);
return responseData;
}
public T Get<T>(string url) where T : class
{
T responseData = null;
try
{
var wi = (WindowsIdentity)HttpContext.Current.User.Identity;
var wic = wi.Impersonate();
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.UseDefaultCredentials = true;
request.Method = HttpMethod.Get.ToString();
request.ContentType = "application/json; charset=utf-8";
string response = GetResponse(request, wic);
// deserialize the response content
JavaScriptSerializer jScriptSerializer = new JavaScriptSerializer();
jScriptSerializer.MaxJsonLength = Int32.MaxValue;
// deserialize the response content
responseData = jScriptSerializer.Deserialize<T>(response);
}
catch (Exception e)
{
}
return responseData;
}
private string GetResponse(HttpWebRequest request, WindowsImpersonationContext wic)
{
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
string responseContent = string.Empty;
string responseStatatusCode = string.Empty;
try
{
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
responseStatatusCode = response.StatusCode.ToString();
if (response.StatusCode == HttpStatusCode.OK)
{
using (Stream responseStream = response.GetResponseStream())
{
StreamReader reader = new StreamReader(responseStream);
responseContent = reader.ReadToEnd();
return responseContent;
}
}
}
catch (WebException webException)
{
wic.Undo();
using (var reader = new StreamReader(webException.Response.GetResponseStream()))
{
responseContent = reader.ReadToEnd();
}
return responseContent;
}
return responseContent;
}
}
}