How to create a simple proxy in C#? Ask Question

How to create a simple proxy in C#? Ask Question

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.

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)?

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?

Update

This is what I understand (see graphic below).

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 The proxy see a new connection, read the HTTP header and see the request he must executes. He executes the request.

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

代替テキスト

Useful link

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 Proxy : I might be able to get some information over here too.

Request reflector : This is a simple example.

Here is a Git Hub Repository with a Simple Http Proxy.

ベストアンサー1

I wouldn't use HttpListener or something like that, in that way you'll come across so many issues.

最も重要なのは、サポートするのが非常に面倒になることです。

  • プロキシキープアライブ
  • SSL は機能しません (正しくは、ポップアップが表示されます)
  • .NET ライブラリは RFC に厳密に従うため、一部のリクエストが失敗します (IE、FF、その他のブラウザは動作しますが)。

必要なことは次のとおりです。

  • TCPポートをリッスンする
  • ブラウザリクエストを解析する
  • 抽出ホストはTCPレベルでそのホストに接続します
  • カスタム ヘッダーなどを追加する場合を除き、すべてを転送します。

私は異なる要件を持つ 2 つの異なる HTTP プロキシを .NET で作成しましたが、これが最善の方法だと言えます。

Mentalis はこれを行っていますが、そのコードは「デリゲート スパゲッティ」であり、GoTo よりもひどいです :)

おすすめ記事