How to add a default WebProxy Module in web.config

By Peter Bromberg

If you are behind a corporate firewall, you probably need to add default proxy settings to your app to allow it to make outbound webrequests. Here is a simple way to add a default webproxy. This works even with things like DataSet.ReadXml( url);

// 1) Add this class either to existing project or separate assembly:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;

namespace YourLib
{
  
      public class MyProxy : IWebProxy
      {
           public ICredentials Credentials
          {
              get { return new NetworkCredential("username", "password","domain"); }
              set { }
          }

          public Uri GetProxy(Uri destination)
          {
               return new Uri("http://my-proxy-address");
          }

          public bool IsBypassed(Uri host)
          {
               return false;
          }
      }      
}


// 2) Add to Web.config:
  <system.net>
    <defaultProxy enabled="true" useDefaultCredentials="false">
      <module type = "YourLib.MyProxy, YourLib" />
    </defaultProxy>
  </system.net>

How to add a default WebProxy Module in web.config  (2883 Views)