-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathHttpPost.cs
More file actions
163 lines (129 loc) · 4.17 KB
/
HttpPost.cs
File metadata and controls
163 lines (129 loc) · 4.17 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
151
152
153
154
155
156
157
158
159
160
161
162
163
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;
using System.Collections;
class HttpPost
{
public HttpPost(string postUri)
{
this.PostUri = postUri;
}
private WebProxy _proxy;
public WebProxy Proxy
{
get { return _proxy; }
set { _proxy = value; }
}
private string _postUri;
public string PostUri
{
get { return _postUri; }
set { _postUri = value; }
}
private string _language;
public string Language
{
get { return _language; }
set { _language = value; }
}
public event EventHandler PostComplete;
public void doPost(PostParamCollection postParamCollection)
{
try
{
Uri uri = new Uri(this.PostUri);
WebRequest webRequest = WebRequest.Create(uri);
webRequest.Headers.Add(HttpRequestHeader.AcceptLanguage, this.Language);
if (this.Proxy != null)
webRequest.Proxy = Proxy;
webRequest.Method = "POST";
webRequest.ContentType = "application/x-www-form-urlencoded";
string parameterString = "";
foreach (PostParam parameter in postParamCollection)
{
parameterString += parameter.Paramter + "=" + parameter.Value + "&";
}
parameterString = parameterString.Substring(0, parameterString.Length - 1);
byte[] byteArray = Encoding.UTF8.GetBytes(parameterString);
webRequest.ContentLength = byteArray.Length;
Stream stream = webRequest.GetRequestStream();
stream.Write(byteArray, 0, byteArray.Length);
stream.Close();
WebResponse webResponse = webRequest.GetResponse();
stream = webResponse.GetResponseStream();
StreamReader streamReader = new StreamReader(stream);
string responseStream = streamReader.ReadToEnd();
webResponse.Close();
streamReader.Close();
if (PostComplete != null)
{
PostComplete.Invoke(responseStream, null);
}
}
catch (Exception exception)
{
throw exception;
}
}
public void doPost(PostParam parameter)
{
try
{
Uri uri = new Uri(this.PostUri);
WebRequest webRequest = WebRequest.Create(uri);
webRequest.Headers.Add(HttpRequestHeader.AcceptLanguage, this.Language);
if (this.Proxy != null)
webRequest.Proxy = Proxy;
webRequest.Method = "POST";
webRequest.ContentType = "application/x-www-form-urlencoded";
string parameterString = "";
parameterString += parameter.Paramter + "=" + parameter.Value + "&";
parameterString = parameterString.Substring(0, parameterString.Length - 1);
byte[] byteArray = Encoding.UTF8.GetBytes(parameterString);
webRequest.ContentLength = byteArray.Length;
Stream stream = webRequest.GetRequestStream();
stream.Write(byteArray, 0, byteArray.Length);
stream.Close();
WebResponse webResponse = webRequest.GetResponse();
stream = webResponse.GetResponseStream();
StreamReader streamReader = new StreamReader(stream);
string responseStream = streamReader.ReadToEnd();
webResponse.Close();
streamReader.Close();
if (PostComplete != null)
{
PostComplete.Invoke(responseStream, null);
}
}
catch (Exception exception)
{
throw exception;
}
}
}
class PostParam
{
public PostParam() { }
public PostParam(string paramter, string value)
{
this.Paramter = paramter;
this.Value = value;
}
private string _paramter;
public string Paramter
{
get { return _paramter; }
set { _paramter = value; }
}
private string _value;
public string Value
{
get { return _value; ; }
set { _value = value; }
}
}
class PostParamCollection : List<PostParam>
{
}