Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Net;
- using System.IO;
- using System.Text;
- public class HttpInterface
- {
- private CookieContainer local_cookies = new CookieContainer();
- 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";
- private bool local_allow_auto_redirect = true;
- private bool local_keep_cookies = true;
- private WebProxy local_proxy = null;
- private int local_timeout = 100000;
- private bool local_ajax = false;
- private string local_error = string.Empty;
- #region Settings
- public string user_agent
- {
- get
- {
- return this.local_user_agent;
- }
- set
- {
- this.local_user_agent = value;
- }
- }
- public string proxy
- {
- get
- {
- return this.local_proxy.Address.Host + ":" + this.local_proxy.Address.Port;
- }
- set
- {
- if (valid_proxy(value))
- {
- string[] splitted = value.Split(':');
- this.local_proxy = new WebProxy(splitted[0], Convert.ToInt32(splitted[1]));
- }
- }
- }
- public int timeout
- {
- get
- {
- return this.local_timeout;
- }
- set
- {
- this.local_timeout = value;
- }
- }
- public bool allow_auto_redirect
- {
- get
- {
- return this.local_allow_auto_redirect;
- }
- set
- {
- this.local_allow_auto_redirect = value;
- }
- }
- public bool keep_cookies
- {
- get
- {
- return this.local_keep_cookies;
- }
- set
- {
- this.local_keep_cookies = value;
- }
- }
- public bool ajax
- {
- get
- {
- return this.local_ajax;
- }
- set
- {
- this.local_ajax = value;
- }
- }
- #endregion
- #region Main
- public string request(string method, string url, string data, string referer)
- {
- this.local_error = string.Empty;
- if (valid_url(url))
- {
- HttpWebResponse response = null;
- StreamReader stream_reader = null;
- try
- {
- HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
- request.Method = (method = method.ToUpper());
- request.Referer = referer;
- request.AllowAutoRedirect = this.local_allow_auto_redirect;
- request.CookieContainer = this.local_cookies;
- request.UserAgent = this.local_user_agent;
- request.Timeout = this.local_timeout;
- request.Proxy = this.local_proxy;
- if (this.local_ajax)
- {
- request.Headers.Add("X-Requested-With", "XMLHttpRequest");
- }
- if (method.Equals("POST"))
- {
- request.ContentType = "application/x-www-form-urlencoded";
- request.ContentLength = data.Length;
- byte[] bytes = Encoding.ASCII.GetBytes(data);
- if (bytes.Length == data.Length)
- {
- Stream data_stream = request.GetRequestStream();
- data_stream.Write(bytes, 0, bytes.Length);
- data_stream.Close();
- }
- }
- response = (HttpWebResponse)request.GetResponse();
- foreach (Cookie cookie in response.Cookies)
- {
- this.local_cookies.Add(cookie);
- }
- stream_reader = new StreamReader(response.GetResponseStream());
- return stream_reader.ReadToEnd();
- }
- catch (Exception ex)
- {
- this.local_error = ex.StackTrace;
- }
- finally
- {
- if (response != null)
- {
- response.Close();
- }
- if (stream_reader != null)
- {
- stream_reader.Close();
- }
- }
- }
- else
- {
- this.local_error = "The URL '" + url + "' is invalid.";
- }
- return string.Empty;
- }
- public string get(string url)
- {
- return this.request("GET", url, string.Empty, string.Empty);
- }
- public string get(string url, string referal)
- {
- return this.request("GET", url, string.Empty, referal);
- }
- public string post(string url, string data)
- {
- return this.request("POST", url, data, string.Empty);
- }
- public string post(string url, string data, string referal)
- {
- return this.request("POST", url, data, referal);
- }
- #endregion
- #region Validate
- public bool valid_url(string url)
- {
- Uri create;
- return Uri.TryCreate(url, UriKind.RelativeOrAbsolute, out create);
- }
- public bool valid_proxy(string proxy)
- {
- string[] splitted = proxy.Split(':');
- if (splitted.Length == 2 && valid_url(splitted[0]))
- {
- try
- {
- new WebProxy(splitted[0], Convert.ToInt32(splitted[1]));
- return true;
- }
- catch { }
- }
- return false;
- }
- #endregion
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement