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?
Security – The proxy can filter and monitor traffic.
Authentication – Only authorized users can access the internet.
Logging & Compliance – All web requests are tracked.
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
Read configuration from web.config
This allows enabling/disabling the proxy without changing code.
Check if proxy is enabled (ProxyFlag == "Y")
Only create the proxy when required.
Create a WebProxy object
Set as default proxy
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
Works in restricted networks – Your app can access the internet where direct connections are blocked.
Flexible configuration – Easily enable or disable proxy in web.config.
Supports authenticated proxies – Handles corporate proxy login credentials.
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.