C#  

Using WebProxy in C# to Access Internet in Restricted Networks

When building C# applications that interact with the internet—such as fetching data from APIs, downloading files, or reading emails—your network environment may restrict direct access to the web. This is common in corporate networks where security, authentication, and logging are enforced.

In such cases, a proxy server acts as an intermediary, allowing your application to communicate with the internet securely and reliably. In this article, we'll explore how to implement a WebProxy in C# and why it's necessary.

What is a Web Proxy?

A proxy server sits between your application and the internet:

[Your App] → [Proxy Server] → [Internet]
[Internet] → [Proxy Server] → [Your App]

Why use a proxy?

  1. Security – The proxy can filter and monitor traffic.

  2. Authentication – Only authorized users can access the internet.

  3. Logging & Compliance – All web requests are tracked.

  4. Network Restrictions – Corporate or ISP firewalls may block direct access.

Without a proxy in a restricted environment, your app may fail to connect to online resources.

C# Implementation: Creating a WebProxy

Here's a reusable method to create and configure a WebProxy in C#:

public static WebProxy CreateProxy()
{
    string proxyFlag = ConfigurationManager.AppSettings["ProxyFlag"];
    if (proxyFlag == "Y")
    {
        string proxyIP = ConfigurationManager.AppSettings["ProxyIP"];
        int proxyPort = Convert.ToInt32(ConfigurationManager.AppSettings["ProxyPort"]);
        string proxyID = ConfigurationManager.AppSettings["ProxyID"];
        string proxyPwd = ConfigurationManager.AppSettings["ProxyPwd"];

        WebProxy myProxy = new WebProxy(proxyIP, proxyPort);
        myProxy.Credentials = new NetworkCredential(proxyID, proxyPwd);

        // Set as default proxy for all web requests
        WebRequest.DefaultWebProxy = myProxy;

        return myProxy;
    }
    return null;
}

Explanation of the code

  1. Read configuration from web.config
    This allows enabling/disabling the proxy without changing code.

  2. Check if proxy is enabled (ProxyFlag == "Y")
    Only create the proxy when required.

  3. Create a WebProxy object

    • Set the proxy server's IP and Port.

    • Assign credentials if authentication is required.

  4. Set as default proxy

    • WebRequest.DefaultWebProxy = myProxy; ensures all HTTP requests use the proxy unless overridden.

Using Proxy with HTTP Requests

Once the proxy is created, you can assign it to a specific HTTP request:

WebProxy proxy = CreateProxy();
if (proxy != null)
    lhttpRequest.Proxy = proxy;
  • lhttpRequest is your HttpWebRequest object.

  • If a proxy exists, it ensures that this particular request uses the proxy server.

Benefits of This Approach

  1. Works in restricted networks – Your app can access the internet where direct connections are blocked.

  2. Flexible configuration – Easily enable or disable proxy in web.config.

  3. Supports authenticated proxies – Handles corporate proxy login credentials.

  4. Centralized control – Setting the default proxy ensures consistent network behavior.

Example web.config Settings

<appSettings>
  <add key="ProxyFlag" value="Y" />
  <add key="ProxyIP" value="192.168.1.100" />
  <add key="ProxyPort" value="8080" />
  <add key="ProxyID" value="username" />
  <add key="ProxyPwd" value="password" />
</appSettings>

Conclusion

Configuring a WebProxy in C# is crucial when working in corporate or restricted network environments. It ensures your application can communicate with the internet securely, supports authentication, and provides flexibility for future network changes.

By using the CreateProxy() method, you can easily integrate proxy support in your C# applications, making them more robust and enterprise-ready.