Here’s a simple and easy class that will ease the pain of configuring a Proxy Connection in your applications.
The way you’d use it is like so:
ProxyInfo pi = new ProxyInfo();
pi.OverrideDefaultProxy=chkOverrideDefaultProxy.Checked;
pi.RequiresLogin= chkLogin.Checked;
pi.BypassLocal=chkBypassLocal.Checked;
pi.ProxyName= txtServerName.Text;
pi.ProxyPort= int.Parse( txtPort.Text);
pi.Domain = txtDomain.Text;
pi.UserId = txtUserName.Text;
pi.Password= txtPassword.Text;
System.Net.WebProxy myProxy = ProxyFactory.Create(pi,WebServiceUrl);
com.regexlib.www.Webservices searcher = new com.regexlib.www.Webservices();
searcher.Proxy= myProxy;
searcher.DoWork();
///
/// Creates a custom initialized Proxy object
/// based on requested settings
///
public class ProxyFactory
{
private ProxyFactory()
{
}
private static void SetCredentials(WebProxy proxy,string user,string password,string domain,string uri)
{
CredentialCache myCache = new CredentialCache();
NetworkCredential myCred =null;
if(domain==null || domain.Length==0)
{ myCred = new NetworkCredential(user,password);}
else
{myCred = new NetworkCredential(user,password,domain);}
myCache.Add(new Uri(uri), "Negotiate", myCred);
proxy.Credentials = myCred;
}
public static WebProxy GetDefaultProxy()
{
return WebProxy.GetDefaultProxy();
}
public static WebProxy Create(string proxyName,int proxyPort,bool bypassLocal)
{
WebProxy proxy = new System.Net.WebProxy(proxyName,proxyPort);
proxy.BypassProxyOnLocal=bypassLocal;
return proxy;
}
public static WebProxy Create(string domain,
string userId,
string password,
string proxyName,
int proxyPort,
bool bypassLocal,
string uri)
{
WebProxy proxy = Create(proxyName,proxyPort,bypassLocal);
SetCredentials(proxy,userId,password,domain,uri);
return proxy;
}
public static WebProxy Create(ProxyInfo settings,string uri)
{
if(!settings.OverrideDefaultProxy)
{
return GetDefaultProxy();
}
if(!settings.RequiresLogin)
{
return Create(settings.ProxyName,settings.ProxyPort,settings.BypassLocal);
}
if(settings.Domain.Trim().Length==0)
{
//return using all the settings without domain
return Create("",
settings.UserId,
settings.Password,
settings.ProxyName,
settings.ProxyPort,
settings.BypassLocal,
uri);
}
//return using all the settings
return Create(settings.Domain,
settings.UserId,
settings.Password,
settings.ProxyName,
settings.ProxyPort,
settings.BypassLocal,
uri);
}
}
///
/// Information holder class
/// To be used by ProxyFactory
///
public class ProxyInfo
{
private bool m_booBypassLocal;
private bool m_booRequiresLogin=false;
private bool m_booOverrideDefaultProxy=false;
private int m_strProxyPort=80;
private string m_strProxyName="";
private string m_strPassword="";
private string m_strDomain="";
private string m_strUserId="";
public ProxyInfo()
{
}
public string UserId
{
get
{return m_strUserId;}
set
{m_strUserId = value;}
}
public string Domain
{
get
{return m_strDomain;}
set
{m_strDomain = value;}
}
public string Password
{
get
{return m_strPassword;}
set
{m_strPassword = value;}
}
public string ProxyName
{
get
{return m_strProxyName;}
set
{m_strProxyName = value;}
}
public int ProxyPort
{
get
{return m_strProxyPort;}
set
{m_strProxyPort = value;}
}
public bool OverrideDefaultProxy
{
get
{return m_booOverrideDefaultProxy;}
set
{m_booOverrideDefaultProxy = value;}
}
public bool RequiresLogin
{
get
{return m_booRequiresLogin;}
set
{m_booRequiresLogin = value;}
}
public bool BypassLocal
{
get
{return m_booBypassLocal;}
set
{m_booBypassLocal = value;}
}
}