我想實現類似於Fiddler的功能。實現一個80端口監聽器
我曾嘗試2個解決方案:
HttpListener
HttpListener listener = new HttpListener();
listener.Prefixes.Add("http://*:80/");
listener.Start();
當start()被執行我得到的錯誤System.Net.HttpListenerException「該進程無法訪問該文件,因爲它正在使用另一個進程「,這意味着有一些其他應用程序在端口80上偵聽IIS或其他任何應用程序。
套接字
IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());
IPAddress ipAddress = ipHostInfo.AddressList[0];
IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 80);
// Create a TCP/IP socket.
Socket listener = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
// Bind the socket to the local endpoint and listen for incoming connections.
try
{
listener.Bind(localEndPoint);
// ..... 當執行listener.Bind()我得到System.Net.Sockets.SocketException(0X80004005):每個套接字地址只能有一個使用(協議/網絡地址/端口)通常允許至極意味着同樣的事情,已經有另一個應用程序偵聽端口80
那麼,如何提琴手偵聽端口80沒有這些問題呢? 我寧願使用像HttpListener這樣的類,因爲它是更高級別的,而不是象套接字這樣的低級別類,我必須實現HTTP協議。
你說得對。我爲傳入連接編寫代碼。我想編寫像Fiddler這樣的傳出連接的代碼。你能給我一個線索怎麼做嗎? –
關於如何在這個SO線程中創建一個代理的一些很好的信息,這應該讓你開始:http://stackoverflow.com/questions/226784/how-to-create-a-simple-proxy-in-c – BrokenGlass