Listen to Browser's requests

后端 未结 6 1734
别跟我提以往
别跟我提以往 2020-12-18 17:22

Using the following code:

HttpListener listener = new HttpListener();
//listener.Prefixes.Add(\"http://*:80/\");
listener.Prefixes.Add(\"http://*:8080/\");
l         


        
相关标签:
6条回答
  • 2020-12-18 17:37

    I dont know, why the GetContext(); hangs, because there is too less information about what happens with the listerner variable, but the problem with port 80 usually is caused by Skype, because it uses port 80 by default. To fix that, open Skype's preferences, go to advanced->connection and uncheck "Use Port 80 and 443 as an alternative for incoming Connections".

    0 讨论(0)
  • 2020-12-18 17:39

    Replace * in your prefixes with +

    listener.Prefixes.Add("http://+:8080/");
    
    0 讨论(0)
  • 2020-12-18 17:41

    this port is probably being used... run netstat -ano on the command line, youll see list of the ports that are being used and the their process ids.

    0 讨论(0)
  • 2020-12-18 17:43

    I would consider looking into this package http://www.nuget.org/packages/Microsoft.AspNet.WebApi.OwinSelfHost/ It uses HttpListener under the covers and with the WebApi HttpMessageHandler it is very easy to create a proxy.

    0 讨论(0)
  • 2020-12-18 17:45

    It hangs because GetContext() is waiting for a request to be received, as said in its documentation:

    This method blocks while waiting for an incoming request. If you want incoming requests to be processed asynchronously (on separate threads) so that your application does not block, use the BeginGetContext method.

    For more info see: https://msdn.microsoft.com/en-us/library/system.net.httplistener.getcontext(v=vs.110).aspx

    Using Asynchronous model tends to be complex, another alternative is running all that code in a different Thread but it depends on your goals.

    0 讨论(0)
  • 2020-12-18 17:46

    @L.B I want to write a "proxy"

    Don't reinvent the wheel and just use the 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();
        }
    }
    

    EDIT

    You can start with this rectangular wheel :)

    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);
                }
            });
    }
    
    0 讨论(0)
提交回复
热议问题