-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRequest.cs
265 lines (222 loc) · 9.01 KB
/
Request.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
namespace AllegroGraphCSharpClient
{
/// <summary>
/// Class to handle the actual communication to the server
/// WCF compatible, so if you use WCF, then you will always need to pass the url to every function
/// Otherwise, you can set it using the constructor
/// </summary>
public class Request : IRESTInterface
{
#region IRESTInterface Members
/// <summary>
/// Generate a HTTP Request object with the specified options
/// </summary>
/// <param name="method"></param>
/// <param name="url"></param>
/// <param name="options"></param>
/// <returns></returns>
public System.Net.HttpWebRequest makeHttpRequest(string method, string url, List<NameValuePairs> options)
{
System.Net.HttpWebRequest webRequest = null;
Dictionary<string, string> query = new Dictionary<string, string>();
try
{
if (options != null)
{
foreach (NameValuePairs np in options)
{
Object value = np.Value;
string key = np.Name;
query.Add(System.Web.HttpUtility.UrlEncode(key), System.Web.HttpUtility.UrlEncode(value.ToString()));
}
}
if ("POST".Equals(method.ToUpper().Trim()))
{
Uri newUrl = new System.Uri(url);
UriBuilder uriB = new UriBuilder(newUrl);
string queryString = string.Empty;
int counter = 0;
foreach (NameValuePairs np in options)
{
queryString += np.Name + "=" + np.Value;
if (counter < options.Count)
{
queryString += "&";
}
}
uriB.Query = queryString;
webRequest = System.Net.WebRequest.Create(uriB.Uri) as System.Net.HttpWebRequest;
webRequest.Method = "POST";
}
else if ("DELETE".Equals(method.ToUpper().Trim())){
Uri newUrl = new Uri(url);
UriBuilder uriB = new UriBuilder(newUrl);
string queryString = string.Empty;
int counter = 0;
foreach (NameValuePairs np in options)
{
queryString += np.Name + "=" + np.Value;
if (counter < options.Count)
{
queryString += "&";
}
}
uriB.Query = queryString;
webRequest = System.Net.WebRequest.Create(uriB.Uri) as System.Net.HttpWebRequest;
webRequest.Method = "DELETE";
}
else if ("PUT".Equals(method.ToUpper().Trim()))
{
Uri newUrl = new Uri(url);
UriBuilder uriB = new UriBuilder(newUrl);
string queryString = string.Empty;
int counter = 0;
foreach (NameValuePairs np in options)
{
queryString += np.Name + "=" + np.Value;
if (counter < options.Count)
{
queryString += "&";
}
}
uriB.Query = queryString;
webRequest = System.Net.WebRequest.Create(uriB.Uri) as System.Net.HttpWebRequest;
webRequest.Method = "PUT";
}
else
{
Uri newUrl = new Uri(url);
UriBuilder uriB = new UriBuilder(newUrl);
string queryString = string.Empty;
int counter = 0;
if (options != null)
{
foreach (NameValuePairs np in options)
{
queryString += np.Name + "=" + np.Value;
if (counter < options.Count)
{
queryString += "&";
}
}
}
uriB.Query = queryString;
webRequest = System.Net.WebRequest.Create(uriB.Uri) as System.Net.HttpWebRequest;
webRequest.Method = "GET";
}
}
catch (Exception ex)
{
System.Diagnostics.Trace.WriteLine("Error in makeHttpRequest: " + ex.Message);
}
return webRequest;
}
/// <summary>
/// Performs a basic REST operation
/// </summary>
/// <param name="method"></param>
/// <param name="url"></param>
/// <param name="options"></param>
/// <param name="ContentType"></param>
/// <param name="accept"></param>
/// <returns></returns>
public List<Results> makeRequest(string method, string url, List<NameValuePairs> options, string ContentType, string accept)
{
List<Results> results = new List<Results>();
try
{
HttpWebRequest request = makeHttpRequest(method, url, options);
if (ContentType == string.Empty)
{
//ebRequest myRequest = WebRequest.Create(url);
// myRequest.Headers.Add("accept", "accept");
if (accept == null)
accept = "application/json";
request.Accept = accept;
}
else
{
request.KeepAlive = true;
request.Accept = accept;
}
try
{
using (WebResponse webResponse = request.GetResponse() as HttpWebResponse)
{
if (webResponse == null)
{
return null;
}
else
{
StreamReader sr = new StreamReader(webResponse.GetResponseStream());
string sb = sr.ReadToEnd().Trim();
Results rs = new Results();
rs.Result = sb;
results.Add(rs);
}
}
}
catch (Exception ex)
{
System.Diagnostics.Trace.WriteLine("Error with posting request: " + ex.Message);
}
}
catch (Exception ex)
{
System.Diagnostics.Trace.WriteLine("error in makeRequest: " + ex.Message);
}
return results;
}
/// <summary>
/// Performs a JSON based REST request
/// </summary>
/// <param name="method"></param>
/// <param name="url"></param>
/// <param name="options"></param>
/// <param name="contentType"></param>
/// <returns></returns>
public List<Results> JSONRequest(string method, string url, List<NameValuePairs> options, string contentType)
{
List<Results> results = new List<Results>();
try
{
results = makeRequest(method, url, options, contentType,"application/json");
}
catch (Exception ex)
{
System.Diagnostics.Trace.WriteLine("Error in JSONRequest: " + ex.Message);
}
return results;
}
/// <summary>
/// Most common way to talk to the server
/// </summary>
/// <param name="method"></param>
/// <param name="url"></param>
/// <param name="options"></param>
/// <param name="contentType"></param>
/// <returns></returns>
public List<Results> StandardRequest(string method, string url, List<NameValuePairs> options, string contentType)
{
List<Results> results = new List<Results>();
try
{
if (contentType == string.Empty)
results = makeRequest(method, url, options, string.Empty, "application/json");
else
results = makeRequest(method, url, options, string.Empty, contentType);
}
catch (Exception ex)
{
System.Diagnostics.Trace.WriteLine("Error in JSONRequest: " + ex.Message);
}
return results;
}
#endregion
}
}