SHOW:
|
|
- or go back to the newest paste.
1 | using System; | |
2 | using System.Net; | |
3 | using System.IO; | |
4 | using System.Text; | |
5 | ||
6 | public class HttpInterface | |
7 | { | |
8 | private CookieContainer local_cookies = new CookieContainer(); | |
9 | private string local_user_agent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.79 Safari/535.11"; | |
10 | private bool local_allow_auto_redirect = true; | |
11 | private bool local_keep_cookies = true; | |
12 | private WebProxy local_proxy = null; | |
13 | private int local_timeout = 100000; | |
14 | private bool local_ajax = false; | |
15 | private string local_error = string.Empty; | |
16 | ||
17 | #region Settings | |
18 | public string user_agent | |
19 | { | |
20 | get | |
21 | { | |
22 | return this.local_user_agent; | |
23 | } | |
24 | set | |
25 | { | |
26 | this.local_user_agent = value; | |
27 | } | |
28 | } | |
29 | ||
30 | public string proxy | |
31 | { | |
32 | get | |
33 | { | |
34 | return this.local_proxy.Address.Host + ":" + this.local_proxy.Address.Port; | |
35 | } | |
36 | set | |
37 | { | |
38 | if (valid_proxy(value)) | |
39 | { | |
40 | string[] splitted = value.Split(':'); | |
41 | this.local_proxy = new WebProxy(splitted[0], Convert.ToInt32(splitted[1])); | |
42 | } | |
43 | } | |
44 | } | |
45 | ||
46 | public int timeout | |
47 | { | |
48 | get | |
49 | { | |
50 | return this.local_timeout; | |
51 | } | |
52 | set | |
53 | { | |
54 | this.local_timeout = value; | |
55 | } | |
56 | } | |
57 | ||
58 | public bool allow_auto_redirect | |
59 | { | |
60 | get | |
61 | { | |
62 | return this.local_allow_auto_redirect; | |
63 | } | |
64 | set | |
65 | { | |
66 | this.local_allow_auto_redirect = value; | |
67 | } | |
68 | } | |
69 | ||
70 | public bool keep_cookies | |
71 | { | |
72 | get | |
73 | { | |
74 | return this.local_keep_cookies; | |
75 | } | |
76 | set | |
77 | { | |
78 | this.local_keep_cookies = value; | |
79 | } | |
80 | } | |
81 | ||
82 | public bool ajax | |
83 | { | |
84 | get | |
85 | { | |
86 | return this.local_ajax; | |
87 | } | |
88 | set | |
89 | { | |
90 | this.local_ajax = value; | |
91 | } | |
92 | } | |
93 | #endregion | |
94 | ||
95 | #region Main | |
96 | public string request(string method, string url, string data, string referer) | |
97 | { | |
98 | this.local_error = string.Empty; | |
99 | if (valid_url(url)) | |
100 | { | |
101 | HttpWebResponse response = null; | |
102 | StreamReader stream_reader = null; | |
103 | try | |
104 | { | |
105 | HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url); | |
106 | request.Method = (method = method.ToUpper()); | |
107 | request.Referer = referer; | |
108 | ||
109 | request.AllowAutoRedirect = this.local_allow_auto_redirect; | |
110 | request.CookieContainer = this.local_cookies; | |
111 | request.UserAgent = this.local_user_agent; | |
112 | request.Timeout = this.local_timeout; | |
113 | request.Proxy = this.local_proxy; | |
114 | if (this.local_ajax) | |
115 | { | |
116 | request.Headers.Add("X-Requested-With", "XMLHttpRequest"); | |
117 | } | |
118 | if (method.Equals("POST")) | |
119 | { | |
120 | request.ContentType = "application/x-www-form-urlencoded"; | |
121 | request.ContentLength = data.Length; | |
122 | byte[] bytes = Encoding.ASCII.GetBytes(data); | |
123 | if (bytes.Length == data.Length) | |
124 | { | |
125 | Stream data_stream = request.GetRequestStream(); | |
126 | data_stream.Write(bytes, 0, bytes.Length); | |
127 | data_stream.Close(); | |
128 | } | |
129 | } | |
130 | response = (HttpWebResponse)request.GetResponse(); | |
131 | foreach (Cookie cookie in response.Cookies) | |
132 | { | |
133 | this.local_cookies.Add(cookie); | |
134 | } | |
135 | stream_reader = new StreamReader(response.GetResponseStream()); | |
136 | return stream_reader.ReadToEnd(); | |
137 | } | |
138 | catch (Exception ex) | |
139 | { | |
140 | this.local_error = ex.StackTrace; | |
141 | } | |
142 | finally | |
143 | { | |
144 | if (response != null) | |
145 | { | |
146 | response.Close(); | |
147 | } | |
148 | if (stream_reader != null) | |
149 | { | |
150 | stream_reader.Close(); | |
151 | } | |
152 | } | |
153 | } | |
154 | else | |
155 | { | |
156 | this.local_error = "The URL '" + url + "' is invalid."; | |
157 | } | |
158 | return string.Empty; | |
159 | } | |
160 | ||
161 | public string get(string url) | |
162 | { | |
163 | return this.request("GET", url, string.Empty, string.Empty); | |
164 | } | |
165 | ||
166 | public string get(string url, string referal) | |
167 | { | |
168 | return this.request("GET", url, string.Empty, referal); | |
169 | } | |
170 | ||
171 | public string post(string url, string data) | |
172 | { | |
173 | return this.request("POST", url, data, string.Empty); | |
174 | } | |
175 | ||
176 | public string post(string url, string data, string referal) | |
177 | { | |
178 | return this.request("POST", url, data, referal); | |
179 | } | |
180 | #endregion | |
181 | ||
182 | #region Validate | |
183 | public bool valid_url(string url) | |
184 | { | |
185 | Uri create; | |
186 | return Uri.TryCreate(url, UriKind.RelativeOrAbsolute, out create); | |
187 | } | |
188 | ||
189 | public bool valid_proxy(string proxy) | |
190 | { | |
191 | string[] splitted = proxy.Split(':'); | |
192 | if (splitted.Length == 2 && valid_url(splitted[0])) | |
193 | { | |
194 | try | |
195 | { | |
196 | ||
197 | new WebProxy(splitted[0], Convert.ToInt32(splitted[1])); | |
198 | return true; | |
199 | } | |
200 | catch { } | |
201 | } | |
202 | return false; | |
203 | } | |
204 | #endregion | |
205 | } |