如何在 C# 中创建一个简单的代理? [英] How to create a simple proxy in C#?

查看:56
本文介绍了如何在 C# 中创建一个简单的代理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几周前我下载了 Privoxy,为了好玩,我很想知道它的简单版本是如何完成的.

I have downloaded Privoxy few weeks ago and for the fun I was curious to know how a simple version of it can be done.

我知道我需要配置浏览器(客户端)以向代理发送请求.代理将请求发送到网络(假设它是一个 http 代理).代理会收到答复...但是代理如何将请求发送回浏览器(客户端)?

I understand that I need to configure the browser (client) to send request to the proxy. The proxy send the request to the web (let say it's a http proxy). The proxy will receive the answer... but how can the proxy send back the request to the browser (client)?

我在网上搜索了 C# 和 http 代理,但没有找到让我理解它在幕后如何正确工作的东西.(我相信我不想要反向代理,但我不确定).

I have search on the web for C# and http proxy but haven't found something that let me understand how it works behind the scene correctly. (I believe I do not want a reverse proxy but I am not sure).

你们有没有一些解释或一些信息可以让我继续这个小项目?

Does any of you have some explication or some information that will let me continue this small project?

这是我的理解(见下图).

This is what I understand (see graphic below).

第 1 步 我配置客户端(浏览器),以便将所有请求发送到 127.0.0.1 代理监听的端口.这样,请求将不会直接发送到 Internet,而是由代理处理.

Step 1 I configure the client (browser) for all request to be send to 127.0.0.1 at the port the Proxy listen. This way, request will be not sent to the Internet directly but will be processed by the proxy.

Step2 代理看到一个新连接,读取 HTTP 标头并查看他必须执行的请求.他执行请求.

Step2 The proxy see a new connection, read the HTTP header and see the request he must executes. He executes the request.

Step3 代理收到来自请求的答复.现在他必须将答案从网络发送到客户端,但如何发送???

Step3 The proxy receive an answer from the request. Now he must send the answer from the web to the client but how???

Mentalis Proxy :我发现这个项目是一个代理(但更多我想要).我可能会检查源,但我真的想要一些基本的东西来了解更多的概念.

Mentalis Proxy : I have found this project that is a proxy (but more that I would like). I might check the source but I really wanted something basic to understand more the concept.

ASP 代理:我也可以从这里获取一些信息.

ASP Proxy : I might be able to get some information over here too.

请求反射器 :这是一个简单的例子.

Request reflector : This is a simple example.

这是一个 带有简单 Http 代理的 Git Hub 存储库.

推荐答案

您可以使用 HttpListener 类来侦听传入的请求和 HttpWebRequest 类来中继请求.

You can build one with the HttpListener class to listen for incoming requests and the HttpWebRequest class to relay the requests.

这篇关于如何在 C# 中创建一个简单的代理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆