forked from ranacseruet/webrequest-csharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmooreaWebRequest.cs
More file actions
150 lines (135 loc) · 4.08 KB
/
mooreaWebRequest.cs
File metadata and controls
150 lines (135 loc) · 4.08 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
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
using System;
using System.Collections.Generic;
using System.Net;
using System.IO;
namespace mooreaSoftware
{
/**
* mooreaWebRequestHeader
* support class to manage custom Headers
**/
class mooreaWebRequestHeader
{
// header name
public string name { get; set; }
// header value
public string value { get; set; }
/**
* mooreaWebRequestHeader init function
**/
public mooreaWebRequestHeader(String name, String value)
{
this.name = name;
this.value = value;
}
}
/**
* mooreaWebRequest
* manage HTTP(S) web requests, encapsulating WebRequest object and functionalities
**/
class mooreaWebRequest
{
// internal WebRequest object
private WebRequest request;
// flag to enable requests to self signed https hosts
public Boolean IgnoreInvalidSSL { get; set; }
// string containing chained parameters for POST requests (example: param01=value¶m02=value)
public String PostParams { get; set; }
// content type for request (if not specified, POST requests use "application/x-www-form-urlencoded" content type)
public String ContentType { get; set; }
// custom headers list
public List<mooreaWebRequestHeader> Headers { get; set; }
// response status
private String status = "";
public String Status { get { return status; } }
/**
* mooreaWebRequest init function
* @param String url address for request
**/
public mooreaWebRequest(string url)
{
// Create a request using a URL that can receive a post.
request = WebRequest.Create(url);
// set init values for class properties
IgnoreInvalidSSL = false;
PostParams = "";
ContentType = "";
Headers = new List<mooreaWebRequestHeader>();
}
/**
* mooreaWebRequest init function (with method)
* @param String url address for request
* @param String method method for request (accepted values: GET, POST)
**/
public mooreaWebRequest(string url, string method)
: this(url)
{
if (method.Equals("GET") || method.Equals("POST"))
{
// Set the Method property of the request to POST.
request.Method = method;
}
else
{
throw new Exception("Invalid Method Type");
}
}
/**
* connect to target url and get response
**/
public string GetResponse()
{
try
{
// if needed, disable SSL certificates verification
if (IgnoreInvalidSSL == true)
{
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
}
// add custom headers
foreach (var header in Headers)
{
request.Headers.Add(header.name, header.value);
}
// if there are post parameters, those are added to the request
if (PostParams != "")
{
// Set the ContentType property of the WebRequest.
if (ContentType == "")
{
request.ContentType = "application/x-www-form-urlencoded";
}
else
{
request.ContentType = ContentType;
}
// add post parameters
using (var streamWriter = new StreamWriter(request.GetRequestStream()))
{
string json = PostParams;
streamWriter.Write(json);
streamWriter.Flush();
streamWriter.Close();
}
}
// Get the original response.
WebResponse response = request.GetResponse();
// set response status
status = ((HttpWebResponse)response).StatusDescription;
// parse response from stream reader
String responseFromServer = "";
using (var streamReader = new StreamReader(response.GetResponseStream()))
{
responseFromServer = streamReader.ReadToEnd();
}
// return response
return responseFromServer;
}
// in case of errors returns them
catch (System.Net.WebException err)
{
return String.Format("error: {0} - {1}", err.Status, err.Message);
}
}
}
}