This is a new HTTPWrapper class built to make the interface between your applications and the online world, simpler. This has new methods of handling requests using a new option interface (littered with the common pre-built options,) along with a new cookie handling method allowing for a more fluid cookie experience.
Seperate - "Split Files";
Contains: HTTPOptions.cs , HTTPInterface.cs
Namespace: HTTPWrapper
Object: HTTPInterface
Unified - Joint Files";
Contains: HTTPWrapper.cs
Namespace: HTTPWrapper
Object: HTTPInterface
This class set was created by Chris Rogers [http://logicalgamers.com], for distribution on http://logicalgamers.com.
This class set may not be redistibuted as a public script if the following requirements are not met;
1. Full information tags are left in-tact (Located In each Class.)
2. Full credit is given.
3. The script is released only on http://logicalgamers.com
Any breaching of this may result in termination of access to http://logicalgamers.com, and possile legal action: depending on the estimated value of damage; However, the use of this class set inside of a compiled C# EXECUTABLE is allowed, and the following requirements are met;
1. The EXECUTABLE is released only on http://logicalgamers.com
Thank you for taking an interest in my class set, I spent an alocated amount of time developing these to be "more suitable" to the virtual needs of the every day developer. If you feel the need, I would greatly appreciate any follow-up you might have about said class set. Please contact me at http://logicalgamers.com (Username: Chris) or at MrWalkingLogic@yahoo.com
Kind Regards,
Chris Rogers
MrWalkingLogic@yahoo.com
Hotfile.com: One click file hosting: HTTPPackage.zip
Hotfile.com: One click file hosting: HTTPPackage.rar
Results 1 to 7 of 7
Thread: [C#] New HTTPWrappers
- 02 Jun. 2011 06:51pm #1
- Age
- 30
- Join Date
- Nov. 2009
- Location
- Anaheim, California
- Posts
- 1,065
- Reputation
- 99
- LCash
- 19.40
[C#] New HTTPWrappers
- 02 Jun. 2011 10:55pm #2
- 05 Jun. 2011 01:28am #3
- Age
- 30
- Join Date
- Nov. 2009
- Location
- Anaheim, California
- Posts
- 1,065
- Reputation
- 99
- LCash
- 1.05
Thanks, I just hope it gets some use. I plan on releasing some new scripts soon.
- 06 Jun. 2011 03:02am #4
This is an example of how easy it is to expand other people's OO code:
Code:using System; using System.Net; using System.Text; using System.IO; namespace HTTPWrapper { public class HTTPInterface_Extended : HTTPInterface { // Added by Riddle [http://logicalgamers.com] private HttpWebResponse httpResponse = null; //Store response from HTTP request; have access to response header, etc. public HttpWebResponse lastResponse { get { return this.httpResponse; //null when failed response } } public HTTPOptions httpOptions { set { this.default_options = value; } } /// <summary> /// Created By: Chris Rogers [http://logicalgamers.com] /// Main request function, actual handler of all interactions. /// Changed a little by Riddle [http://logicalgamers.com] /// </summary> public string request(HTTPOptions options) { string result = string.Empty; try { Stream stream = null; StreamReader stream_reader = null; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(options.url); request.Method = (options.type) ? "GET" : "POST"; request.UserAgent = options.user_agent; if (options.ajax) { request.Headers.Add("X-Requested-With", "XMLHttpRequest"); } if (options.proxy != null) { request.Proxy = options.proxy; } if (options.read_cookies) { request.CookieContainer = this.cookie_container; } if (!options.type) { byte[] databytes = Encoding.UTF8.GetBytes(options.data); request.ContentType = "application/x-www-form-urlencoded"; request.ContentLength = databytes.Length; stream = request.GetRequestStream(); stream.Write(databytes, 0, databytes.Length); stream.Close(); } HttpWebResponse response = (HttpWebResponse)request.GetResponse(); httpResponse = (options.saveResponse) ? response:null; //added by Riddle if (options.write_cookies) { this.write_cookie_container(response.Cookies); } stream = response.GetResponseStream(); stream_reader = new StreamReader(stream); result = stream_reader.ReadToEnd(); stream_reader.Close(); stream.Close(); } catch (Exception e) { this.httpResponse = null; //added by Riddle throw e; } return result; } } }
Anyways, this is just supposed to be an example of how easy is to extend OO code if needed. I have not tested this, so it might not work.
- 06 Jun. 2011 06:29pm #5
- Age
- 30
- Join Date
- Nov. 2009
- Location
- Anaheim, California
- Posts
- 1,065
- Reputation
- 99
- LCash
- 1.46
- 06 Jun. 2011 10:40pm #6
No problem.
Another thing you could implement is importing and exporting HTTPOptions objects (serialized perhaps? or even just a settings file!). This way you don't have to restrict yourself to GET and POST requests only. It would still go through port 80 (standard HTTP port), but you could have completely customized headers. Since there's more to HTTP than just GET and POST, it might be useful for some people to have that. Of course, I doubt that would apply to this kind of environment (HTML based games), since all you really need is GET and POST.
Another thing I would add is the referer header. Usually HTTP Wrappers give the option to assign the referer at will, others handle it automatically (with enable/disable options).
- 05 Aug. 2011 10:22pm #7
So it has been done (some odd months later)! I also added the old codes I have to GetStringBetween (Between) and GetStringsBetween (BetweenAll).
Code:/* * Created By: Chris Rogers [http://logicalgamers.com] * Created On: 05/30/2011 * Created For: Simple unification of HTTPWrapper classes. */ using System; using System.Net; using System.Text; using System.IO; using System.Collections.Generic; namespace HTTPWrapper { #region HTTPInterface /* * Created By: Chris Rogers [http://logicalgamers.com] * Created On: 05/01/2011 * Created For: Easy HTTP get/post/request methods. * Features: - GET * - POST * - REQUEST * - URL Proxy Support * - (Disabling/Enabling) of Cookie (Reading/Writting) * - AJAX Header Munipulation. * - User Agent Header Munipulation. */ class HTTPInterface { private HTTPOptions default_options = new HTTPOptions ( ); private CookieContainer cookie_container = new CookieContainer ( ); /// <summary> /// Main request function, actual handler of all interactions. /// </summary> /// public string Between(string Input, string Start, string End) { return this.Between(Input, Start, End, 0); } public string Between(string Input, string Start, string End, int 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> BetweenAll(string Input, string Start, string End) { List<string> Values = new List<String>(); int Offset = 0; while (true) { if (Input.Length > 0 && Start.Length > 0 && End.Length > 0 && Offset < Input.Length) { int StartPos = (Input.IndexOf(Start, Offset) + Start.Length); if ((StartPos - Start.Length) > -1 && Input.Length >= StartPos) { int Length = (Input.IndexOf(End, StartPos) - StartPos); if (Length > -1) { Values.Add(Input.Substring(StartPos, Length)); Offset = StartPos + Length; continue; } } } break; } return Values; } public string Request ( HTTPOptions options ) { string result = string.Empty; try { Stream stream = null; StreamReader stream_reader = null; HttpWebRequest request = ( HttpWebRequest ) WebRequest.Create ( options.url ); request.Method = ( options.type ) ? "GET" : "POST"; request.Referer = options.referer; request.UserAgent = options.user_agent; if ( options.ajax ) { request.Headers.Add ( "X-Requested-With", "XMLHttpRequest" ); } if ( options.proxy != null ) { request.Proxy = options.proxy; } if ( options.read_cookies ) { request.CookieContainer = this.cookie_container; } if ( !options.type ) { byte[] databytes = Encoding.UTF8.GetBytes ( options.data ); request.ContentType = "application/x-www-form-urlencoded"; request.ContentLength = databytes.Length; stream = request.GetRequestStream ( ); stream.Write ( databytes, 0, databytes.Length ); stream.Close ( ); } HttpWebResponse response = ( HttpWebResponse ) request.GetResponse ( ); if ( options.write_cookies ) { this.write_cookie_container ( response.Cookies ); } stream = response.GetResponseStream ( ); stream_reader = new StreamReader ( stream ); result = stream_reader.ReadToEnd ( ); stream_reader.Close ( ); stream.Close ( ); } catch ( Exception e ) { throw e; } return result; } #region GET /// <summary> /// Sends a GET method http request. /// </summary> public string Get ( string url ) { return this.Get ( url, false, null ); } public string Get(string url, string referer) { return this.Get(url, false, referer); } /// <summary> /// Sends a GET method http request. /// </summary> public string Get ( string url, bool ajax, string referer ) { HTTPOptions options = this.default_options.clone ( ); options.type = true; options.url = url; options.referer = referer; options.ajax = ajax; return this.Request ( options ); } #endregion #region POST /// <summary> /// Sends a POST method http request. /// </summary> public string Post ( string url ) { return this.Post ( url, string.Empty, false, null ); } public string Post(string url, string data, string referer) { return this.Post(url, data, false, referer); } /// <summary> /// Sends a POST method http request. /// </summary> public string Post ( string url, bool ajax ) { return this.Post ( url, string.Empty, ajax, null ); } /// <summary> /// Sends a POST method http request. /// </summary> public string Post ( string url, string data ) { return this.Post ( url, data, false, null ); } /// <summary> /// Sends a POST method http request. /// </summary> public string Post ( string url, string data, bool ajax, string referer ) { HTTPOptions options = this.default_options.clone ( ); options.type = false; options.url = url; options.data = data; options.ajax = ajax; options.referer = referer; return this.Request ( options ); } #endregion private void write_cookie_container ( CookieCollection cookie_collection ) { foreach ( Cookie cookie in cookie_collection ) { this.cookie_container.Add ( cookie ); } } #region Public Options /// <summary> /// True to enable the reading and writting of cookies, False to disable the reading and writting of cookies. /// </summary> public void cookies ( bool use ) { this.default_options.read_cookies = use; this.default_options.write_cookies = use; } /// <summary> /// True to enable the reading of cookies, False to disable the reading of cookies. /// </summary> public void read_cookies ( bool use ) { this.default_options.read_cookies = use; } /// <summary> /// True to enable the writting of cookies, False to disable the writting of cookies. /// </summary> public void write_cookies ( bool use ) { this.default_options.write_cookies = use; } /// <summary> /// Should be a valid user agent. /// </summary> public void user_agent ( string user_agent ) { this.default_options.user_agent = user_agent; } /// <summary> /// Must be a valid proxy url. /// </summary> public void proxy ( string url ) { this.default_options.proxy_url = url; } #endregion } #endregion #region HTTPOptions /* * Created By: Chris Rogers [http://logicalgamers.com] * Created On: 05/01/2011 * Created For: Simple method of option storage for HTTPInterface.cs */ class HTTPOptions { #pragma warning disable #region Local Variables private bool local_type = true; private string local_referer = null; private string local_url = string.Empty; private string local_data = string.Empty; private bool local_ajax = false; private bool local_read_cookies = true; private bool local_write_cookies = true; private string local_user_agent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.10 (KHTML, like Gecko) Chrome/8.0.552.224 Safari/534.10"; private WebProxy local_proxy = null; #endregion #region Variable Functions /// <summary> /// True for GET requests, and False for POST requests. /// </summary> public string referer { get { return local_referer; } set { this.local_referer = value; } } public bool type { get { return local_type; } set { this.local_type = value; } } /// <summary> /// Must be a valid URL including 'http://' or 'https://' prefixes. /// </summary> public string url { get { return local_url; } set { try { if ( value.Length > 0 ) { value = value.ToLower ( ); if ( value.IndexOf ( "http://", 0 ) != 0 && value.IndexOf ( "https://", 0 ) != 0 ) { throw new FormatException ( "HTTPOptions.url must start with 'http://' or 'https://'." ); } Uri URI = new Uri ( value ); } this.local_url = value; } catch ( ArgumentNullException e ) { throw new ArgumentNullException ( "HTTPOptions.url must not be null." ); } catch ( UriFormatException e ) { throw new UriFormatException ( "HTTPOptions.url must be a valid url." ); } } } /// <summary> /// Must not include the '?', and must also be formatted like so: 'name=value&name1=value1'. /// </summary> public string data { get { return local_data; } set { this.local_data = value; } } /// <summary> /// True to send with AJAX headers, and False to send without AJAX headers. /// </summary> public bool ajax { get { return local_ajax; } set { this.local_ajax = value; } } /// <summary> /// True to enable cookies to be sent to next viewed page, and False to disable the cookies from being sent. /// </summary> public bool read_cookies { get { return local_read_cookies; } set { this.local_read_cookies = value; } } /// <summary> /// True to enable cookies to be received, and possibly overwrite some cookies, and False to disable the cookies from being received. /// </summary> public bool write_cookies { get { return local_write_cookies; } set { this.local_write_cookies = value; } } /// <summary> /// Should be a valid user agent. /// </summary> public string user_agent { get { return local_user_agent; } set { this.local_user_agent = value; } } /// <summary> /// Must be a valid System.Net.WebProxy instance. /// </summary> public WebProxy proxy { get { return local_proxy; } set { this.local_proxy = value; } } /// <summary> /// Must be a valid proxy url. /// </summary> public string proxy_url { set { try { this.local_proxy = new WebProxy ( value, true ); } catch ( UriFormatException e ) { throw new UriFormatException ( "HTTPOptions.proxy must be a valid url." ); } } } #endregion /// <summary> /// Returns a duplicated instance of this HTTPOptions instance. /// </summary> public HTTPOptions clone ( ) { HTTPOptions result = new HTTPOptions ( ); result.type = this.type; result.url = this.url; result.data = this.data; result.ajax = this.ajax; result.read_cookies = this.read_cookies; result.write_cookies = this.write_cookies; result.user_agent = this.user_agent; result.proxy = this.proxy; return result; } } #endregion } /* This class set was created by Chris Rogers [http://logicalgamers.com], for distribution on http://logicalgamers.com. This class set may not be redistibuted as a public script if the following requirements are not met; 1. Full information tags are left in-tact (Located In each Class.) 2. Full credit is given. 3. The script is released only on http://logicalgamers.com Any breaching of this may result in termination of access to http://logicalgamers.com, and possile legal action: depending on the estimated value of damage; However, the use of this class set inside of a compiled C# EXECUTABLE is allowed, and the following requirements are met; 1. The EXECUTABLE is released only on http://logicalgamers.com Thank you for taking an interest in my class set, I spent an alocated amount of time developing these to be "more suitable" to the virtual needs of the every day developer. If you feel the need, I would greatly appreciate any follow-up you might have about said class set. Please contact me at http://logicalgamers.com (Username: Chris) or at MrWalkingLogic@yahoo.com Kind Regards, Chris Rogers MrWalkingLogic@yahoo.com */
EDIT: Removed the quotes in the post because I mixed up the code tags. Updated post with proper code and accidentally forgot to put the referrer in the posting method, fixed.