Popular blog tags

Implementing a .net Forward proxy server in C#(HTTP-CONNECT proxy)

Published

 

Creating a new C# project

 

Change Global Forward Proxy using C# .NET

 

Step by Step

Create a Forward Proxy project using C# .NET

Create a .NET console application using Visual Studio

 

You can set your proxy in Internet Options to point to PuppyProxy for both HTTP and HTTPS on the default IP address of 127.0.0.1 and port 8000

 

To run a proxy server in a C# application, you will need to use the HttpListener class, which provides a simple way to create and listen for HTTP requests in your application. Here is an example of how you might use this class to create a simple proxy server,

// Create a new HttpListener to listen for requests on the specified URL
HttpListener listener = new HttpListener();
listener.Prefixes.Add("http://localhost:8080/");
listener.Start();
// Wait for a request to be made to the server
HttpListenerContext context = listener.GetContext();
// Get the request and response objects
HttpListenerRequest request = context.Request;
HttpListenerResponse response = context.Response;
// Modify the request as needed (e.g. to add headers, change the URL, etc.)
// ...
// Forward the request to the destination server
HttpWebRequest destinationRequest = (HttpWebRequest) WebRequest.Create(request.Url);
// Copy the request headers from the original request to the new request
foreach(string header in request.Headers) {
    destinationRequest.Headers[header] = request.Headers[header];
}
// Get the response from the destination server
HttpWebResponse destinationResponse = (HttpWebResponse) destinationRequest.GetResponse();
// Copy the response headers from the destination response to the client response
foreach(string header in destinationResponse.Headers) {
    response.Headers[header] = destinationResponse.Headers[header];
}
// Get the response stream from the destination response and copy it to the client response
using(Stream destinationStream = destinationResponse.GetResponseStream())
using(Stream outputStream = response.OutputStream) {
    destinationStream.CopyTo(outputStream);
}
// Close the response and the listener
response.Close();
listener.Close();

 

https://www.c-sharpcorner.com/article/implementing-a-proxy-server-in-c-sharp-an-example-and-test-case/

 

修改windows 系统注册表

RegistryKey registry = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", true);
registry.SetValue("ProxyEnable", 1);
registry.SetValue("ProxyServer", "127.0.0.1:8080");

blog:https://stackoverflow.com/questions/2020363/how-to-change-global-windows-proxy-using-c-sharp-net-with-immediate-effect

https://blog.csdn.net/zcr_59186/article/details/129940223