using System;
using System.IO;
using System.Net;
namespace AppLimit.CloudComputing.SharpBox.Common.Net.Web
{
///
/// This delegate can be used to defines callbacks which will be called after
/// a webrequest is processed
///
/// the caller of the webrequest
/// The executed evemt args
public delegate void WebRequestExecuted(object sender, WebRequestExecutedEventArgs e);
///
/// This delegate can be used to defines callbacks which will be called before
/// a webrequest is processed
///
/// the caller of the webrequest
/// the executed event args
public delegate void WebRequestExecuting(object sender, WebRequestExecutingEventArgs e);
///
/// This class allows to be informed about all oAuth requests in the
/// library. Do not add event receiver during runtime, only during start
/// up of your application
///
public sealed class WebRequestManager
{
#region Singleton Stuff
private static readonly WebRequestManager instance = new WebRequestManager();
// Explicit static constructor to tell C# compiler
// not to mark type as beforefieldinit
static WebRequestManager()
{
}
private WebRequestManager()
{
_webProxySettings = null;
}
///
/// This is the instance accessor of the oAuthServiceManager
///
public static WebRequestManager Instance
{
get { return instance; }
}
#endregion
private IWebProxy _webProxySettings;
///
/// This event will be raised when the webrequest is prepared
///
public event WebRequestExecuting RequestPreparedEvent;
///
/// This event will beraised before a webrequest will be executed
///
public event WebRequestExecuting RequestExecutingEvent;
///
/// This event will be raised after a webrequest was executed
///
public event WebRequestExecuted RequestExecutedEvent;
internal void NotifyWebRequestPrepared(WebRequest r)
{
if (RequestPreparedEvent != null)
{
var e = new WebRequestExecutingEventArgs() { request = r };
RequestPreparedEvent(this, e);
}
}
internal void NotifyWebRequestExecuting(WebRequest r)
{
if (RequestExecutingEvent != null)
{
var e = new WebRequestExecutingEventArgs() { request = r };
RequestExecutingEvent(this, e);
}
}
internal void NotifyWebRequestExecuted(WebResponse response, TimeSpan timeNeeded, Stream resultStream, WebException exception)
{
if (RequestExecutedEvent != null)
{
var e = new WebRequestExecutedEventArgs
{
response = response,
timeNeeded = timeNeeded,
resultStream = resultStream,
exception = exception
};
RequestExecutedEvent(this, e);
}
}
///
/// This method allows to set an alternative proxy host. The system wide
/// settings wil be overwritten
///
///
///
///
public void SetProxySettings(string proxyHost, int proxyPort, ICredentials credentials)
{
if (proxyHost != null)
{
_webProxySettings = new WebProxy(proxyHost, proxyPort);
_webProxySettings.Credentials = credentials;
}
else
_webProxySettings = null;
}
internal IWebProxy GetProxySettings()
{
return _webProxySettings;
}
///
/// This method remove the proxy settings and ensures that no proxy will be used
/// at all
///
public void SetEmptyProxySettings()
{
_webProxySettings = new WebRequestManagerNullProxy();
}
}
}