[C#] HttpInterface Class (Perfected wrappers, proxy support, simple structure.)
I haven't made many releases lately so I recently went back over my wrappers and cleaned up a lot of the junk coding, and went ahead and did a nice bit of stress testing to make sure they work as perfectly as I could get them to.
I hope everyone finds this release useful!
HttpInterface.cs
Code:
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 = false;
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 url_data )
{
return this.request ( "POST", url, url_data, string.Empty );
}
public string post ( string url, string url_data, string referal )
{
return this.request ( "POST", url, url_data, referal );
}
#endregion
#region Validate
public bool valid_url ( string url )
{
try
{
new Uri ( url );
return true;
}
catch { }
return false;
}
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
}
And for anyone who needs gsb (GetStringBetween) or gasb (GetAllStringsBetween) functionality here is a replica of the infamous StringExtensions.cs class :]
StringExtensions.cs
Code:
using System.Collections.Generic;
public static class StringExtensions
{
public static string gsb ( this string input, string start, string end )
{
return input.gsb ( start, end, 0 );
}
public static string gsb ( this string input, string start, string end, int offset )
{
if ( offset < input.Length )
{
int startpos = ( input.IndexOf ( start, offset ) + start.Length );
if ( ( startpos - start.Length ) > -1 )
{
int endpos = input.IndexOf ( end, startpos );
if ( endpos > -1 )
{
return input.Substring ( startpos, ( endpos - startpos ) );
}
}
}
return string.Empty;
}
public static List<string> gasb ( this string input, string start, string end )
{
List<string> result = new List<string> ( );
int offset = 0;
while ( true )
{
if ( offset < input.Length )
{
int startpos = ( input.IndexOf ( start, offset ) + start.Length );
if ( ( startpos - start.Length ) > -1 )
{
int endpos = input.IndexOf ( end, startpos );
if ( endpos > -1 )
{
result.Add ( input.Substring ( startpos, ( endpos - startpos ) ) );
offset = endpos;
continue;
}
}
}
break;
}
return result;
}
}
Also here is an example usage of the HttpInterface.cs script!
Program.cs (Console Application)
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
class Program
{
static void Main ( string [ ] args )
{
HttpInterface wrapper = new HttpInterface ( );
Console.WriteLine ( wrapper.get ( "http://google.com/" ) );
Console.ReadLine ( );
}
}