/
WebExtensions.cs
259 lines (219 loc) · 10.3 KB
/
WebExtensions.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
namespace System
{
public static class WebExtensions
{
public static string GetResponseString(this HttpWebRequest req)
{
return req.GetResponseString(null, null, null, null, null);
}
public static string GetResponseString(this HttpWebRequest req, string pData)
{
return req.GetResponseString(pData, null, null, null, null);
}
public static string GetResponseString(this HttpWebRequest req, string ConsumerKey, string ConsumerSecret, string AccessToken, string AccessTokenSecret)
{
return req.GetResponseString(null, ConsumerKey, ConsumerSecret, AccessToken, AccessTokenSecret);
}
public static string GetResponseString(this HttpWebRequest req, string pData, string ConsumerKey, string ConsumerSecret, string AccessToken, string AccessTokenSecret)
{
if (req != null)
{
//req.Method = "GET";//Get is the default anyway.
if (!string.IsNullOrEmpty(pData))
{
if (req.Method == "GET")//don't change the method if it has been set manually.
req.Method = "POST";
using (System.IO.Stream reqStream = req.GetRequestStream())
{
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] data = encoding.GetBytes(pData);
reqStream.Write(data, 0, data.Length);
}
}
if (!string.IsNullOrEmpty(ConsumerKey) && !string.IsNullOrEmpty(ConsumerSecret))
{
req.AddOAuth(pData, ConsumerKey, ConsumerSecret, AccessToken, AccessTokenSecret);
}
try
{
using (HttpWebResponse resp = req.GetResponse() as HttpWebResponse)
{
using (System.IO.Stream st = resp.GetResponseStream())
{
using (System.IO.StreamReader sr = new System.IO.StreamReader(st))
{
return sr.ReadToEnd();
}
}
}
}
catch (WebException ex)
{
if (ex.Status == WebExceptionStatus.ProtocolError)
{
HttpWebResponse r = ex.Response as HttpWebResponse;
if (r != null)
{
throw new ProtocolException((ApiStatusCode)r.StatusCode, ex.Message);
}
}
throw;
}
}
return "";
}
public static HttpWebRequest AddOAuth(this HttpWebRequest req, string ConsumerKey, string ConsumerSecret, string AccessToken, string AccessTokenSecret)
{
return req.AddOAuth("", ConsumerKey, ConsumerSecret, AccessToken, AccessTokenSecret);
}
public static HttpWebRequest AddOAuth(this HttpWebRequest req, string pData, string ConsumerKey, string ConsumerSecret, string AccessToken, string AccessTokenSecret)
{
string nonce = new Random().Next(Int16.MaxValue, Int32.MaxValue).ToString("X", System.Globalization.CultureInfo.InvariantCulture);
string timeStamp = DateTime.UtcNow.ToUnixTimeStamp().ToString();
// Create the base string. This is the string that will be hashed for the signature.
Dictionary<string, string> param = new Dictionary<string, string>();
param.Add("oauth_consumer_key", ConsumerKey);
param.Add("oauth_nonce", nonce);
param.Add("oauth_signature_method", "HMAC-SHA1");
param.Add("oauth_timestamp", timeStamp);
if (!string.IsNullOrEmpty(AccessToken))
param.Add("oauth_token", AccessToken);
param.Add("oauth_version", "1.0");
pData += req.RequestUri.Query;
foreach (string kv in pData.Replace("?", "&").Split('&'))
{
string[] akv = kv.Split('=');
if (akv.Length == 2)
{
param.Add(akv[0], akv[1].PercentDecode());
}
}
StringBuilder sParam = new StringBuilder(); ;
foreach (KeyValuePair<string, string> p in param.OrderBy(k => k.Key))
{
if (sParam.Length > 0)
sParam.Append("&");
sParam.AppendFormat("{0}={1}", p.Key.PercentEncode(), p.Value.PercentEncode());
}
string url = req.RequestUri.AbsoluteUri;
if (!string.IsNullOrEmpty(req.RequestUri.Query))
{
url = url.Replace(req.RequestUri.Query, "");
}
string signatureBaseString
= string.Format("{0}&{1}&{2}",
req.Method.ToUpper(),
url.PercentEncode(),
sParam.ToString().PercentEncode()
);
// Create our hash key (you might say this is a password)
string signatureKey = string.Format("{0}&{1}", ConsumerSecret.PercentEncode(), AccessTokenSecret.PercentEncode());
// Generate the hash
System.Security.Cryptography.HMACSHA1 hmacsha1 = new System.Security.Cryptography.HMACSHA1(Encoding.UTF8.GetBytes(signatureKey));
byte[] signatureBytes = hmacsha1.ComputeHash(Encoding.UTF8.GetBytes(signatureBaseString));
string signature = Convert.ToBase64String(signatureBytes).PercentEncode();
string at = string.IsNullOrEmpty(AccessToken) ? "" : string.Format("oauth_token=\"{0}\",", AccessToken);
string oauth = "OAuth realm=\"{0}\",oauth_consumer_key=\"{1}\",oauth_nonce=\"{2}\",oauth_signature_method=\"HMAC-SHA1\",oauth_timestamp=\"{3}\",{4}oauth_version=\"1.0\",oauth_signature=\"{5}\"";
oauth = string.Format(oauth, "Spiral16", ConsumerKey, nonce, timeStamp, at, signature);
req.Headers.Add("Authorization", oauth);
req.ContentType = "application/x-www-form-urlencoded";
return req;
}
public static HttpWebRequest SignRequest(this HttpWebRequest req, string pData, string singingKey)
{
string nonce = new Random().Next(Int16.MaxValue, Int32.MaxValue).ToString("X", System.Globalization.CultureInfo.InvariantCulture);
string timeStamp = DateTime.UtcNow.ToUnixTimeStamp().ToString();
// Create the base string. This is the string that will be hashed for the signature.
Dictionary<string, string> param = new Dictionary<string, string>();
param.Add("oauth_nonce", nonce);
param.Add("oauth_signature_method", "HMAC-SHA512");
param.Add("oauth_timestamp", timeStamp);
pData += req.RequestUri.Query;
foreach (string kv in pData.Replace("?", "&").Split('&'))
{
string[] akv = kv.Split('=');
if (akv.Length == 2)
{
param.Add(akv[0], akv[1].PercentDecode());
}
}
StringBuilder sParam = new StringBuilder(); ;
foreach (KeyValuePair<string, string> p in param.OrderBy(k => k.Key))
{
if (sParam.Length > 0)
sParam.Append("&");
sParam.AppendFormat("{0}={1}", p.Key.PercentEncode(), p.Value.PercentEncode());
}
string url = req.RequestUri.AbsoluteUri;
if (!string.IsNullOrEmpty(req.RequestUri.Query))
{
url = url.Replace(req.RequestUri.Query, "");
}
string signatureBaseString
= string.Format("{0}&{1}&{2}",
req.Method.ToUpper(),
url.PercentEncode(),
sParam.ToString().PercentEncode()
);
// Generate the hash
System.Security.Cryptography.HMACSHA512 hmacsha512 = new System.Security.Cryptography.HMACSHA512(Encoding.UTF8.GetBytes(singingKey));
byte[] signatureBytes = hmacsha512.ComputeHash(Encoding.UTF8.GetBytes(signatureBaseString));
string signature = Convert.ToBase64String(signatureBytes).PercentEncode();
req.Headers.Add("oauth_nonce", nonce);
req.Headers.Add("oauth_signature_method", "HMAC-SHA512");
req.Headers.Add("oauth_timestamp", timeStamp);
req.Headers.Add("oauth_signature", signature);
req.ContentType = "application/x-www-form-urlencoded";
return req;
}
//public static bool CheckRequestSignature(this System.Web.HttpRequest req, string singingKey)
//{
// int? nonce = req.Headers["oauth_nonce"].ToInt();
// var signature_method = req.Headers["oauth_signature_method"];
// var timestamp = req.Headers["oauth_timestamp"];
// string signature = req.Headers["oauth_signature"];
// signature = signature = signature.PercentDecode();
// if (nonce.HasValue && StateManagement.Get<int?>(nonce.Value.ToString()).HasValue)
// return false;
// StateManagement.Add(nonce.Value.ToString(), nonce, 525600);
// return true;
//}
}
public enum ApiStatusCode : long
{
BadRequest = 400,
Unauthorized = 401,
Forbidden = 403,
NotFound = 404,
Gone = 410,
TooManyRequest = 429,
UnprocessableEntity = 422,
InternalServerError = 500,
ServiceUnavailable = 503
}
public class ProtocolException : WebException
{
public ApiStatusCode StatusCode { get; set; }
public ProtocolException()
: base()
{ }
public ProtocolException(string message)
: base(message)
{ }
public ProtocolException(ApiStatusCode statusCode)
: base()
{
this.StatusCode = statusCode;
}
public ProtocolException(ApiStatusCode statusCode, string message)
: base(message)
{
this.StatusCode = statusCode;
}
}
}