I want to implement functionality similar to Fiddler.
I have tried 2 solutions:
HttpListener
HttpListener listener = new HttpListener();
listener.Prefixes.Add("http://*:80/");
listener.Start();
When Start()
is executed I get the error:
System.Net.HttpListenerException "The process cannot access the file because it is being used by another process"
which means that there is some other application listening on port 80 maybe IIS or whatever.
Sockets
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);
// .....
When listener.Bind()
is executed I get:
System.Net.Sockets.SocketException (0x80004005): Only one usage of each socket address (protocol/network address/port) is normally permitted
which means the same thing, there is already another application listening on port 80.
So how does Fiddler listen to port 80 without these problems? I would prefer to use a class like HttpListener because it's higher level instead of a lower level class like socket where I have to implement the HTTP Protocol.