---
title: "Easy Proxy handling through the ProxyFactory and ProxyInfo classes"
url: "https://osherove.com/blog/2003/10/30/easy-proxy-handling-through-the-proxyfactory-and-proxyinfo-c.md"
canonical: "https://osherove.com/blog/2003/10/30/easy-proxy-handling-through-the-proxyfactory-and-proxyinfo-c.html"
published: "2003-10-30T13:38:00"
updated: "2003-10-30T13:38:00"
tags: ["Other"]
author: "Roy Osherove"
description: "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 =…"
usage: "Licensed under CC BY 4.0 — reuse, quote and adapt with attribution (Roy Osherove, link to the original page)."
license: "https://creativecommons.org/licenses/by/4.0/"
---

# Easy Proxy handling through the ProxyFactory and ProxyInfo classes

By Roy Osherove October 30, 2003 In [Other](https://osherove.com/blog/category/other)

**Historical article** · Originally published October 30, 2003; technical details may be outdated.

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;}

            }

      }

## More in Other

[All posts in Other](https://osherove.com/blog/category/other)

- Jan 17, 2004

    ### [Learning from mistakes: the Don Quixote pattern](https://osherove.com/blog/2004/1/17/learning-from-mistakes-the-don-quixote-pattern.html)

- Dec 25, 2003

    ### [An optimizing experience](https://osherove.com/blog/2003/12/25/an-optimizing-experience.html)

- Sep 9, 2003

    ### [My favorites in OPML](https://osherove.com/blog/2003/9/9/my-favorites-in-opml.html)

- Jun 11, 2003

    ### [Announcing: .Net Desktops Contest](https://osherove.com/blog/2003/6/11/announcing-net-desktops-contest.html)


---

## About the author

**Roy Osherove**: Senior Solutions Architect at AWS working on AI for production and AI-driven development; author of The Art of Unit Testing and Elastic Leadership, with about 20 years of consulting and training on unit testing, TDD, team leadership and continuous delivery.

### Sites

- [RobotPaper](https://robotpaper.ai/): This blog: AI patterns and anti-patterns, AI and coding agents, and engineering leadership.
- [osherove.com](https://osherove.com): Roy's personal site. Author of The Art of Unit Testing and Elastic Leadership; consulting and training on unit testing, TDD, team leadership and continuous delivery, plus about 20 years of blog posts.
- [The Art of Unit Testing](https://www.artofunittesting.com): The Art of Unit Testing, 3rd Edition (Manning): readable, maintainable, trustworthy unit tests, plus TDD workshops in C#, JavaScript, Java and Kotlin.
- [5whys.com](https://www.5whys.com): Roy's writing on software team leadership: self-organizing teams, surviving chaos, learning mode and the Elastic Leadership model.
- [Pipeline Driven](https://pipelinedriven.org): Continuous delivery and DevOps practices: build patterns, pipeline-friendly testing, DevOps metrics and enterprise DevOps.
- [Elastic Leadership](https://www.elasticleadership.com): Elastic Leadership, Roy's framework for software team leaders (survival, learning and self-organizing modes).

### Social

- X: https://x.com/royosherove
- LinkedIn: https://www.linkedin.com/in/osherove/

### Contact

- Contact form: https://forms.gle/jQSx5KoXvrVA1JCK7

### License

Licensed under CC BY 4.0 — reuse, quote and adapt with attribution (Roy Osherove, link to the original page). License: https://creativecommons.org/licenses/by/4.0/
