-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathHttpClient.cs
176 lines (120 loc) · 5.36 KB
/
HttpClient.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Collections.Specialized;
using System.Threading;
using Spring.Collections.Specialized;
using Spring.Util;
namespace InstaSharp {
public static class HttpClient {
private static ManualResetEvent allDone = new ManualResetEvent(false);
private static HttpWebRequest request;
private static WebRequest webRequest;
private static HttpWebResponse response;
private static string requestResult = string.Empty;
public static string GET(string uri) {
try {
request = (HttpWebRequest) WebRequest.Create(uri);
request.Method = "GET";
request.BeginGetResponse(new AsyncCallback(GetResponseCallback), request);
allDone.Reset();
allDone.WaitOne();
return ReadResponse(response.GetResponseStream());
}
catch (WebException ex) {
return ReadResponse(ex.Response.GetResponseStream());
}
}
private static void GetResponseCallback(IAsyncResult asynchronousResult)
{
try
{
request = (HttpWebRequest)asynchronousResult.AsyncState;
response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);
allDone.Set();
}
catch (Exception e)
{
//Debug.WriteLine("Got Exception in GetResponseCallback: " + e.Message);
}
}
public static string POST(string url) {
return POST(url, new Dictionary<string, string>());
}
public static string POST(string url, IDictionary<string, string> args) {
try {
WebClient client = new WebClient();
client.Encoding = Encoding.UTF8;
StringBuilder formattedParams = new StringBuilder();
IDictionary<string, string> parameters = new Dictionary<string, string>();
foreach (var arg in args) {
parameters.Add(arg.Key, arg.Value);
formattedParams.AppendFormat("{0}={1}&", arg.Key, HttpUtility.UrlEncode(arg.Value));
}
//Formatted URI
// Uri baseUri = new Uri(url + "?" + formattedParams.ToString().Substring(0, formattedParams.Length - 1));
client.Headers[HttpRequestHeader.ContentLength] = formattedParams.Length.ToString();
client.UploadStringCompleted += new UploadStringCompletedEventHandler(client_UploadStringCompleted);
client.UploadStringAsync(new Uri(url), "POST",formattedParams.ToString());
allDone.Reset();
allDone.WaitOne();
return requestResult;
}
catch (WebException ex) {
return ReadResponse(ex.Response.GetResponseStream());
}
}
static void client_UploadStringCompleted(object sender, UploadStringCompletedEventArgs e)
{
requestResult = e.Result;
allDone.Set();
}
public static string DELETE(string uri) {
var request = HttpWebRequest.Create(uri);
request.Method = "DELETE";
return DELETE(uri, new Dictionary<string, string>());
// return ReadResponse(request.GetResponse().GetResponseStream());
}
public static string DELETE(string uri, IDictionary<string, string> args) {
try {
WebClient client = new WebClient();
client.Encoding = Encoding.UTF8;
client.Headers["Connection"] = "Keep-Alive";
StringBuilder formattedParams = new StringBuilder();
IDictionary<string, string> parameters = new Dictionary<string, string>();
foreach (var arg in args)
{
parameters.Add(arg.Key, arg.Value);
formattedParams.AppendFormat("{0}={{{1}}}", arg.Key, arg.Key);
}
//Formatted URI
Uri baseUri = new Uri(uri);
client.UploadStringCompleted += new UploadStringCompletedEventHandler(client_UploadStringCompletedDelete);
client.UploadStringAsync(baseUri, "DELETE", string.Empty);
allDone.Reset();
allDone.WaitOne();
return requestResult;
}
catch (WebException ex) {
return ReadResponse(ex.Response.GetResponseStream());
}
}
static void client_UploadStringCompletedDelete(object sender, UploadStringCompletedEventArgs e)
{
requestResult = e.Result;
allDone.Set();
}
private static string ReadResponse(Stream response) {
StreamReader reader = new StreamReader(response);
string line;
StringBuilder result = new StringBuilder();
while ((line = reader.ReadLine()) != null) {
result.Append(line);
}
return result.ToString();
}
}
}