How to Build a Simple HTTP Proxy in CSharp in just 25 lines of code

Oct 1, 2023 ยท 3 min read

It's easy to build a basic proxy server in C# using the .NET framework. The key is using the HttpListener and WebClient classes.

First we need to add the necessary namespaces:

using System;
using System.Net;
using System.IO;

Then we create an HttpListener to listen for incoming requests:

HttpListener listener = new HttpListener();
listener.Prefixes.Add("<http://localhost:9097/>");
listener.Start();

Next we need an event handler when a request comes in:

listener.BeginGetContext(new AsyncCallback(HandleRequest), listener);

The handler method will fetch the target URL and stream it back to the client:

static void HandleRequest(IAsyncResult result) {
  HttpListener listener = (HttpListener)result.AsyncState;

  HttpListenerContext context = listener.EndGetContext(result);

  string url = context.Request.Url.AbsolutePath.Substring(1);

  WebClient client = new WebClient();
  context.Response.OutputStream.Write(client.DownloadData(url), 0, client.DownloadData(url).Length);

  context.Response.OutputStream.Close();
}

We slice off the leading slash and use WebClient to download and stream back the response.

Putting it all together:

using System;
using System.Net;
using System.IO;

class Proxy {

  static void Main() {
    HttpListener listener = new HttpListener();
    listener.Prefixes.Add("<http://localhost:9097/>");
    listener.Start();

    listener.BeginGetContext(new AsyncCallback(HandleRequest), listener);

    Console.ReadLine();
    listener.Close();
  }

  static void HandleRequest(IAsyncResult result) {
    //...handle request
  }

}

This gives you a simple 25 line proxy server in C#. Of course there is still a lot more you would want to add for a robust production proxy. But it shows how easy the .NET framework makes building basic network apps.

This is great as a learning exercise but it is easy to see that even the proxy server itself is prone to get blocked as it uses a single IP. In this scenario where you may want a proxy that handles thousands of fetches every day using a professional rotating proxy service to rotate IPs is almost a must.

Otherwise, you tend to get IP blocked a lot by automatic location, usage, and bot detection algorithms.

Our rotating proxy server Proxies API provides a simple API that can solve all IP Blocking problems instantly.

  • With millions of high speed rotating proxies located all over the world,
  • With our automatic IP rotation
  • With our automatic User-Agent-String rotation (which simulates requests from different, valid web browsers and web browser versions)
  • With our automatic CAPTCHA solving technology,
  • Hundreds of our customers have successfully solved the headache of IP blocks with a simple API.

    The whole thing can be accessed by a simple API like below in any programming language.

    In fact, you don't even have to take the pain of loading Puppeteer as we render Javascript behind the scenes and you can just get the data and parse it any language like Node, Puppeteer or PHP or using any framework like Scrapy or Nutch. In all these cases you can just call the URL with render support like so:

    curl "<http://api.proxiesapi.com/?key=API_KEY&render=true&url=https://example.com>"
    

    We have a running offer of 1000 API calls completely free. Register and get your free API Key.

    Browse by tags:

    Browse by language:

    Tired of getting blocked while scraping the web?

    ProxiesAPI handles headless browsers and rotates proxies for you.
    Get access to 1,000 free API credits, no credit card required!