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;
}
}
}
I added functionality to get the last HttpWebResponse. In my case, I would probably use it to debug or something along those lines, but I'm sure other people could find it useful for more than that. I also added the option to define your own HTTPOptions object used in HTTPInterface. This could be useful if the user wants to use custom headers for a request or something along those lines, in this case it would be possible to use request() directly by adding what I like to call a "proxy" function (just makes sure that everything is correct before passing the object to the private function request()).
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.