监听浏览器的请求

isp*_*iro 0 .net c# http httplistener

使用以下代码:

HttpListener listener = new HttpListener();
//listener.Prefixes.Add("http://*:80/");
listener.Prefixes.Add("http://*:8080/");
listener.Prefixes.Add("http://*:8081/");
listener.Prefixes.Add("http://*:8082/");
listener.Start();
HttpListenerContext context = listener.GetContext();
HttpListenerRequest request = context.Request;
Run Code Online (Sandbox Code Playgroud)

GetContext();尽管在 IE 和 Firefox 中加载 http(不是 https)页面,但该程序仍会挂起。

当我取消注释第一行时,出现错误:

无法侦听前缀“http://*:80/”,因为它与计算机上的现有注册冲突。

那么如何监听浏览器的请求呢?

L.B*_*L.B 5

@LB我想写一个“代理”

不要重新发明轮子,只需使用FiddlerCore

public class HttpProxy : IDisposable
{
    public HttpProxy()
    {
        Fiddler.FiddlerApplication.BeforeRequest += FiddlerApplication_BeforeRequest;
        Fiddler.FiddlerApplication.Startup(8764, true, true);
    }

    void FiddlerApplication_BeforeRequest(Fiddler.Session oSession)
    {
        Console.WriteLine(String.Format("REQ: {0}", oSession.url));
    }

    public void Dispose()
    {
        Fiddler.FiddlerApplication.Shutdown();
    }
}
Run Code Online (Sandbox Code Playgroud)

编辑

你可以从这个矩形轮开始:)

void SniffPort80()
{
    byte[] input = new byte[] { 1 };
    Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.IP);
    socket.Bind(new IPEndPoint(IPAddress.Broadcast, 80));
    socket.IOControl(IOControlCode.ReceiveAll, input, null);

    byte[] buffer = new byte[0x10000];

    Task.Factory.StartNew(() =>
        {
            while (true)
            {
                int len = socket.Receive(buffer);
                if (len <= 40) continue; //Poor man's check for TCP payload
                string bin = Encoding.UTF8.GetString(buffer, 0, len); //Don't trust to this line. Encoding may be different :) even it can contain binary data like images, videos etc.
                Console.WriteLine(bin);
            }
        });
}
Run Code Online (Sandbox Code Playgroud)

  • 没有。我需要自己写。这就是问题的重点。 (2认同)