-
Notifications
You must be signed in to change notification settings - Fork 14
/
Http.cs
209 lines (176 loc) · 6.82 KB
/
Http.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
using System;
using System.IO;
using System.Net;
using System.Text;
using System.Security.Cryptography;
public static class Http
{
static string ByteToString(byte[] buff)
{
string sbinary = "";
for (int i = 0; i < buff.Length; i++)
{
sbinary += buff[i].ToString("X2"); /* hex format */
}
return (sbinary);
}
public static readonly Object objLock = new object();
public static readonly Object objLock2 = new object();
public static string post(String url, String parameters, bool fast = false)
{
string _ret = null;
while (_ret == null)
{
_ret = _post(url, parameters);
if (_ret == null)
System.Threading.Thread.Sleep(new Random().Next(1000, 1200));
}
return _ret;
}
public static string _post(String url, String parameters, bool fast = false)
{
try
{
// lock (objLock)
{
Logger.log(url + parameters);
var request = (HttpWebRequest)WebRequest.Create(url);
//System.Threading.Thread.Sleep(1000);
parameters = "nonce=" + (decimal.Parse(DateTime.Now.ToString("yyyyMMddHHmmssfffff"))) + "&" + parameters;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
var data = Encoding.ASCII.GetBytes(parameters);
HMACSHA512 encryptor = new HMACSHA512();
encryptor.Key = Encoding.ASCII.GetBytes(Key.getSecret());
String sign = ByteToString(encryptor.ComputeHash(data));
request.Headers["Key"] = Key.getKey();
request.Headers["Sign"] = sign;
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;
using (var stream = request.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
var response = (HttpWebResponse)request.GetResponse();
String result = new StreamReader(response.GetResponseStream()).ReadToEnd();
Logger.log(result);
System.Threading.Thread.Sleep(new Random().Next(2000, 3000));
return result;
}
}
catch (Exception ex)
{
Logger.log("ERROR POST " + ex.Message + ex.StackTrace);
return null;
}
finally
{
}
}
public static string postGeneric(String url, String parameters)
{
try
{
// lock (objLock)
{
System.Threading.Thread.Sleep(new Random().Next(2000, 3500));
Logger.log(url + parameters);
var request = (HttpWebRequest)WebRequest.Create(url);
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
var data = Encoding.ASCII.GetBytes(parameters);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;
using (var stream = request.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
var response = (HttpWebResponse)request.GetResponse();
String result = new StreamReader(response.GetResponseStream()).ReadToEnd();
Logger.log(result);
return result;
}
}
catch (Exception ex)
{
Logger.log("ERROR POST " + ex.Message + ex.StackTrace);
return null;
}
finally
{
}
}
public static string GerarHashMd5(string input)
{
MD5 md5Hash = MD5.Create();
// Converter a String para array de bytes, que é como a biblioteca trabalha.
byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input));
// Cria-se um StringBuilder para recompôr a string.
StringBuilder sBuilder = new StringBuilder();
// Loop para formatar cada byte como uma String em hexadecimal
for (int i = 0; i < data.Length; i++)
{
sBuilder.Append(data[i].ToString("x2"));
}
return sBuilder.ToString();
}
public static string getCache(String url, bool wait = true)
{
try
{
String aux = GerarHashMd5(url);
if (System.IO.File.Exists(Program.location + @"\cache\" + aux + ".txt"))
return System.IO.File.ReadAllText(Program.location + @"\cache\" + aux + ".txt");
Console.WriteLine("Wait 6s...");
System.Threading.Thread.Sleep(new Random().Next(5000, 6000));
String r = "";
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
var httpWebRequest = (HttpWebRequest)WebRequest.Create(new Uri(url));
httpWebRequest.Method = "GET";
var httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
var responseStream = httpWebResponse.GetResponseStream();
if (responseStream != null)
{
var streamReader = new StreamReader(responseStream);
r = streamReader.ReadToEnd();
}
if (responseStream != null) responseStream.Close();
//Console.WriteLine(r);
System.IO.StreamWriter w = new StreamWriter(Program.location + @"\cache\" + aux + ".txt",true);
w.Write(r);
w.Close();
w.Dispose();
return r;
}
catch (WebException ex)
{
return null;
}
}
public static string get(String url,bool wait = true)
{
try
{
String r = "";
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
var httpWebRequest = (HttpWebRequest)WebRequest.Create(new Uri(url));
httpWebRequest.Method = "GET";
var httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
var responseStream = httpWebResponse.GetResponseStream();
if (responseStream != null)
{
var streamReader = new StreamReader(responseStream);
r = streamReader.ReadToEnd();
}
if (responseStream != null) responseStream.Close();
//Console.WriteLine(r);
return r;
}
catch (WebException ex)
{
return null;
}
}
}