A preserved archive of the Logical Gamers community forums, 2009-2025. The original threads and posts, served read-only. Registration, posting and private messages are gone for good.

C# HTTPWrappers [Private]

1.2k views · started by Chris ·
#1
C# HTTPWrappers [Private]
using System;
using System.Text;
using System.Net;
using System.IO;
using System.Collections;
using System.Diagnostics;
using System.Collections.Generic;

public class HTTPWrapper
{
private CookieContainer Cookies = new CookieContainer();
private Hashtable rawCookies = new Hashtable();
private String Proxy = String.Empty;
private String User_Agent = String.Empty;

public String post(String URL)
{
return post(URL, "", "");
}

public String post(String URL, String Data)
{
return post(URL, Data, "");
}

public String post(String URL, String Data, String Referer)
{
HttpWebRequest Request = (HttpWebRequest)WebRequest.Create(URL);
Request.Method = "POST";
Request.ContentType = "application/x-www-form-urlencoded";
Request.Referer = Referer;
Request.ContentLength = Data.Length;
Request.CookieContainer = Cookies;

try
{
Request.UserAgent = (User_Agent != String.Empty) ? User_Agent : null;
}
finally
{
Request.UserAgent = null;
}

try
{
Request.Proxy = (Proxy != String.Empty) ? new WebProxy(Proxy) : null;
}
finally
{
Request.Proxy = null;
}

try
{
Stream rs = Request.GetRequestStream();
byte[] Bytes = Encoding.ASCII.GetBytes(Data);
rs.Write(Bytes, 0, Bytes.Length);
rs.Close();

HttpWebResponse Response = (HttpWebResponse)Request.GetResponse();
for (int i = 0; i < Response.Cookies.Count; i++)
{
Cookies.Add(Response.Cookies[i]);
if (rawCookies.ContainsKey(Response.Cookies[i].Name)) rawCookies.Remove(Response.Cookies[i].Name);
rawCookies.Add(Response.Cookies[i].Name, Response.Cookies[i].Value);
}
return (new StreamReader(Response.GetResponseStream())).ReadToEnd();
}
catch (Exception ex)
{
Debug.Write(ex.Message);
return String.Empty;
}
}

public String get(String URL)
{
return get(URL, "");
}

public String get(String URL, String Referer)
{
HttpWebRequest Request = (HttpWebRequest)WebRequest.Create(URL);
Request.Method = "GET";
Request.Referer = Referer;
Request.CookieContainer = Cookies;

try
{
Request.UserAgent = (User_Agent != String.Empty) ? User_Agent : null;
}
finally
{
Request.UserAgent = null;
}

try
{
Request.Proxy = (Proxy != String.Empty) ? new WebProxy(Proxy) : null;
}
finally
{
Request.Proxy = null;
}

try
{
HttpWebResponse Response = (HttpWebResponse)Request.GetResponse();
for (int i = 0; i < Response.Cookies.Count; i++)
{
Cookies.Add(Response.Cookies[i]);
if (rawCookies.ContainsKey(Response.Cookies[i].Name)) rawCookies.Remove(Response.Cookies[i].Name);
rawCookies.Add(Response.Cookies[i].Name, Response.Cookies[i].Value);
}
return (new StreamReader(Response.GetResponseStream())).ReadToEnd();
}
catch (Exception ex)
{
Debug.Write(ex.Message);
return String.Empty;
}
}

public Stream get_stream(String URL)
{
return get_stream(URL, "");
}

public Stream get_stream(String URL, String Referer)
{
HttpWebRequest Request = (HttpWebRequest)WebRequest.Create(URL);
Request.Method = "GET";
Request.Referer = Referer;
Request.CookieContainer = Cookies;

try
{
Request.UserAgent = (User_Agent != String.Empty) ? User_Agent : null;
}
finally
{
Request.UserAgent = null;
}

try
{
Request.Proxy = (Proxy != String.Empty) ? new WebProxy(Proxy) : null;
}
finally
{
Request.Proxy = null;
}

try
{
HttpWebResponse Response = (HttpWebResponse)Request.GetResponse();
for (int i = 0; i < Response.Cookies.Count; i++)
{
Cookies.Add(Response.Cookies[i]);
if (rawCookies.ContainsKey(Response.Cookies[i].Name)) rawCookies.Remove(Response.Cookies[i].Name);
rawCookies.Add(Response.Cookies[i].Name, Response.Cookies[i].Value);
}
return Response.GetResponseStream();
}
catch (Exception ex)
{
Debug.Write(ex.Message);
return null;
}
}

public CookieContainer cookies
{
get {
return Cookies;
}
set {
this.Cookies = value;
}
}

public String cookie(String Name)
{
return ((rawCookies.ContainsKey(Name)) ? (String)rawCookies[name] : String.Empty);
}

public String proxy
{
get
{
return Proxy;
}
set
{
this.Proxy = value;
}
}

public String user_agent
{
get
{
return User_Agent;
}
set
{
this.User_Agent = value;
}
}

public String between(String Input, String Start, String End)
{
return between(Input, Start, End, 0);
}

public String between(String Input, String Start, String End, Int32 Offset)
{
if (Input.Length > 0 && Start.Length > 0 && End.Length > 0 && Offset < Input.Length)
{
Int32 StartPos = (Input.IndexOf(Start, Offset) + Start.Length);
if ((StartPos - Start.Length) > -1 && Input.Length >= StartPos)
{
Int32 Length = (Input.IndexOf(End, StartPos) - StartPos);
if (Length > -1)
{
return Input.Substring(StartPos, Length);
}
}
}
return String.Empty;
}

public List<String> all_between(String Input, String Start, String End)
{
List<String> Values = new List<String>();
Int32 Offset = 0;
while (true)
{
if (Input.Length > 0 && Start.Length > 0 && End.Length > 0 && Offset < Input.Length)
{
Int32 StartPos = (Input.IndexOf(Start, Offset) + Start.Length);
if ((StartPos - Start.Length) > -1 && Input.Length >= StartPos)
{
Int32 Length = (Input.IndexOf(End, StartPos) - StartPos);
if (Length > -1)
{
Values.Add(Input.Substring(StartPos, Length));
Offset = StartPos + Length;
continue;
}
}
}
break;
}
return Values;
}
}[/name][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i]


Enjoy.
#2
Sexy. Does this support GZip?
#3
Riddle wrote:
Sexy. Does this support GZip?


It SHOULD, I am not positive.
#4
I would figure that the Request class would handle that. :P
#5
Riddle wrote:
I would figure that the Request class would handle that. :P


Yeah, that is what I meant. I believe the class handles it itself, but I don't believe I've tried.
#6
Updated, added an extremely important fix for user_agents and proxies. This will prevent errors if the user inputs invalid information.
#7
Proxy demo please.
#8
HTTPWrapper Wrapper = new HTTPWrapper();
Wrapper.proxy = "127.0.0.1:2626"

Wrapper.get("http://whatismyip.com");
#9
Just an update;
These do support HTTPS. Found it out because I'm going to make a small school project. Just thought this news could help some people.
#10
Chad wrote:
Just an update;
These do support HTTPS. Found it out because I'm going to make a small school project. Just thought this news could help some people.


THANK JESUS.

Time to work on what I've wanted to do for oh so long!
#11
Chad wrote:
Just an update;
These do support HTTPS. Found it out because I'm going to make a small school project. Just thought this news could help some people.


Lol, whatcha doin in school that requires wrappers for C#?
#12
GamerForce wrote:
Lol, whatcha doin in school that requires wrappers for C#?


Well I hate Vb.net and I love C# and PHP and of course my school is so gay that they decided to say, "We aren't allowed to host PHP files on our server." so I'm stuck with C# and I wanted to make a program (just for me) to auto get my grades when I login on my school computer.
#13
Oh, i forgot, you guys have windows. Lucky >.> I hate this freakin mac pros. (At school right now.) What a waste of money these macs are.
#14
http://www.hackforums.net/showthread.php?tid=687178

If you decompile them in .NET Reflector it's Chris's exact code aside from the LastPage.
Bman will never learn.