-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWeb.cs
53 lines (43 loc) · 1.45 KB
/
Web.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
using System;
using System.IO;
using System.Net;
using System.Text;
class Web
{
public static string GetPost(string Url, params string[] postdata)
{
string result = string.Empty;
string data = string.Empty;
System.Text.ASCIIEncoding ascii = new ASCIIEncoding();
if (postdata.Length % 2 != 0)
{
return "Parameters must be even , \"user\" , \"value\" , ... etc";
}
for (int i = 0; i < postdata.Length; i += 2)
{
data += string.Format("&{0}={1}", postdata[i], postdata[i + 1]);
}
data = data.Remove(0, 1);
byte[] bytesarr = ascii.GetBytes(data);
try
{
WebRequest request = WebRequest.Create(Url);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = bytesarr.Length;
System.IO.Stream streamwriter = request.GetRequestStream();
streamwriter.Write(bytesarr, 0, bytesarr.Length);
streamwriter.Close();
WebResponse response = request.GetResponse();
streamwriter = response.GetResponseStream();
System.IO.StreamReader streamread = new System.IO.StreamReader(streamwriter);
result = streamread.ReadToEnd();
streamread.Close();
}
catch (Exception ex)
{
return ex.Message;
}
return result;
}
}