[C#] WebInterface ( Seamless HTTP/HTTPS Wrappers )
I am not quite positive of the length of my stay; however, it did last a while. To bribe my way back into the hearts of the community here is a recent piece of work that should go great with your next applications! This is the WebInterface Class. It is a seamless HTTP/HTTPS web wrapper, which allows you to access HTTP and HTTPS web pages as a web browser would. It is useful in the development of website automation and exploitation software, because it allows the developer to mimic the actions of a web browser, and thus a person.
Variables;
bool debug : Write source code to debug stream.
string user_agent : User Agent, allows you to spoof what your visible user agent is.
int timeout : The amount of time to wait for a page to load. In milliseconds ( 1000 milliseconds = 1 second )
bool allow_redirect : Allow automatic redirection after request url.
string web_proxy : Proxy String - format "IP
ORT"
HttpStatusCode status_code : Last HTTP status code.
string status_description : Last HTTP status description.
Functions;
string get ( string url, string referer = null, bool xmlhttprequest = false )
string post ( string url, string query_string = null, string referer = null, bool xmlhttprequest = false )
string request ( string url, string query_string = null, Methods method = Methods.GET, string referer = null, bool xmlhttprequest = false )
clear ( bool cookies = true )
Variables;
bool debug : Write source code to debug stream.
string user_agent : User Agent, allows you to spoof what your visible user agent is.
int timeout : The amount of time to wait for a page to load. In milliseconds ( 1000 milliseconds = 1 second )
bool allow_redirect : Allow automatic redirection after request url.
string web_proxy : Proxy String - format "IP
ORT"HttpStatusCode status_code : Last HTTP status code.
string status_description : Last HTTP status description.
Functions;
string get ( string url, string referer = null, bool xmlhttprequest = false )
string post ( string url, string query_string = null, string referer = null, bool xmlhttprequest = false )
string request ( string url, string query_string = null, Methods method = Methods.GET, string referer = null, bool xmlhttprequest = false )
clear ( bool cookies = true )
using System;
using System.Net;
using System.IO;
using System.Text;
using System.Diagnostics;
public class WebInterface
{
public enum Methods
{
GET,
POST
}
// Master Options
public bool debug { get; set; }
// Basic HTTP Options
public string user_agent { get; set; }
public int timeout { get; set; }
public bool allow_redirect { get; set; }
// Cookie Jar - To maintain cookies.
public CookieContainer cookie_container { get; set; }
// Web Proxy, for easy string -> WebProxy / WebProxy -> string conversions.
private WebProxy local_web_proxy;
public string web_proxy
{
get
{
return ( local_web_proxy != null ) ? this.local_web_proxy.Address.Host + ":" + this.local_web_proxy.Address.Port : string.Empty;
}
set
{
this.local_web_proxy = ( this.web_proxy != value ) ? valid_proxy ( value ) : null;
}
}
// Last HTTP Status Code & Description.
public HttpStatusCode status_code { get; private set; }
public string status_description { get; private set; }
public WebInterface ( bool debug = false )
{
ServicePointManager.ServerCertificateValidationCallback += delegate { return true; };
this.user_agent = null;
this.timeout = 100000;
this.allow_redirect = false;
this.debug = debug;
clear ( );
}
public string get ( string url, string referer = null, bool xmlhttprequest = false )
{
return request ( url, null, Methods.GET, referer, xmlhttprequest );
}
public string post ( string url, string query_string = null, string referer = null, bool xmlhttprequest = false )
{
return request ( url, query_string, Methods.POST, referer, xmlhttprequest );
}
public string request ( string url, string query_string = null, Methods method = Methods.GET, string referer = null, bool xmlhttprequest = false )
{
string result = string.Empty;
Uri uri;
if ( ( uri = valid_url ( url ) ) != null )
{
WebRequest web_request;
try
{
// Create WebRequest
web_request = WebRequest.Create ( uri );
// Set Method and Timeout
web_request.Method = method == Methods.GET ? "GET" : "POST";
web_request.Timeout = timeout;
// WebProxy
if ( local_web_proxy != null )
{
web_request.Proxy = local_web_proxy;
}
// Spoof XMLHttpRequest Header
if ( xmlhttprequest )
{
web_request.Headers.Add ( "X-Requested-With", "XMLHttpRequest" );
}
// HTTP Items
( ( HttpWebRequest ) web_request ).AllowAutoRedirect = allow_redirect;
( ( HttpWebRequest ) web_request ).Referer = referer;
( ( HttpWebRequest ) web_request ).UserAgent = user_agent;
( ( HttpWebRequest ) web_request ).CookieContainer = cookie_container;
// Handle sending the POST query string
if ( method == Methods.POST )
{
// Query string -> bytes
byte [ ] bytes = Encoding.UTF8.GetBytes ( query_string );
// Set headers
web_request.ContentType = "application/x-www-form-urlencoded";
web_request.ContentLength = bytes.Length;
// Send bytes
using ( Stream stream = web_request.GetRequestStream ( ) )
{
stream.Write ( bytes, 0, bytes.Length );
}
}
// Get WebResponse from WebRequest
using ( WebResponse web_response = web_request.GetResponse ( ) )
{
// Set Status Code & Description
this.status_code = ( ( HttpWebResponse ) web_response ).StatusCode;
this.status_description = ( ( HttpWebResponse ) web_response ).StatusDescription;
// Handle new cookies
CookieCollection cookie_collection = ( ( HttpWebResponse ) web_response ).Cookies;
foreach ( Cookie cookie in cookie_collection )
{
cookie_container.Add ( cookie );
}
// Get StreamReader from WebResponse
using ( Stream stream = web_response.GetResponseStream ( ) )
{
using ( StreamReader stream_reader = new StreamReader ( stream ) )
{
result = stream_reader.ReadToEnd ( );
}
}
}
}
catch { }
}
if ( debug )
{
Debug.WriteLine ( url );
}
return result;
}
public void clear ( bool cookies = true )
{
cookie_container = ( cookies ) ? new CookieContainer ( ) : cookie_container;
}
#region Validation
private Uri valid_url ( string url )
{
try
{
return new Uri ( url );
}
catch { }
return null;
}
private WebProxy valid_proxy ( string proxy )
{
try
{
string [ ] splitted = proxy.Trim ( ).Split ( ':' );
return new WebProxy ( splitted [0], Convert.ToInt32 ( splitted [1] ) );
}
catch { }
return null;
}
#endregion
}[/1][/0]