Http Monitor in C#

2019-09-17 06:59发布

this is my code for Monitoring Http:

static void Main(string[] args)
{
     try
     {
          byte[] input = BitConverter.GetBytes(1);
          byte[] buffer = new byte[4096];
          Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.IP);
          s.Bind(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 80));
          s.IOControl(IOControlCode.ReceiveAll, input, null);
          s.BeginReceive(arrResponseBytes, 0, arrResponseBytes.Length, SocketFlags.None, new AsyncCallback(OnClientReceive), s);
          System.Threading.ManualResetEvent reset = new System.Threading.ManualResetEvent(false);
          reset.WaitOne();
      }
      catch (Exception ex)
      {
          Console.WriteLine(ex);
      }
      Console.ReadKey();
}

static byte[] arrResponseBytes = new byte[1024 * 5];
protected static void OnClientReceive(IAsyncResult ar)
{
     Socket socket = (Socket)ar.AsyncState;
     int count = socket.EndReceive(ar);
     if (count > 0)
     {
          Console.WriteLine(Encoding.ASCII.GetString(arrResponseBytes, 0, count));
          socket.BeginReceive(arrResponseBytes, 0, arrResponseBytes.Length, SocketFlags.None, new AsyncCallback(OnClientReceive), socket);
     }
}

but i cannot get http hosts. I do not know what data. i want to get http host for example: http://google.com how can i monitor system http? thanks.

1条回答
戒情不戒烟
2楼-- · 2019-09-17 07:22

What you see in your link is the IP + TCP headers. You should parse the IP&TCP headers to extract the content. TCP content starts approximately at offset 40. So you can try your program's modified version as below to see the content of each HTTP request. (Working but not complete program just to give you an idea)

PS: See the s.Bind(new IPEndPoint(IPAddress.Broadcast, 80));

static void Main(string[] args)
{
    try
    {
        byte[] input = new byte[]{1};
        byte[] buffer = new byte[4096];
        Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.IP);
        s.Bind(new IPEndPoint(IPAddress.Broadcast, 80));
        s.IOControl(IOControlCode.ReceiveAll , input, null);
        s.BeginReceive(arrResponseBytes, 0, arrResponseBytes.Length, SocketFlags.None, new AsyncCallback(OnClientReceive), s);
        System.Threading.ManualResetEvent reset = new System.Threading.ManualResetEvent(false);
        reset.WaitOne();
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex);
    }
    Console.ReadKey();
}

static byte[] arrResponseBytes = new byte[1024 * 64];
static void OnClientReceive(IAsyncResult ar)
{
    Socket socket = (Socket)ar.AsyncState;
    int count = socket.EndReceive(ar);
    if (count >= 40)
    {
        try
        {
            string s = Encoding.UTF8.GetString(arrResponseBytes, 40, count - 40);
            string bin = BitConverter.ToString(arrResponseBytes, 40, count - 40).Replace("-", " ");
            if(s.StartsWith("GET"))
                Console.WriteLine(s + " - " + bin);
            //Thread.Sleep(1000);
        }
        catch { }
    }
    socket.BeginReceive(arrResponseBytes, 0, arrResponseBytes.Length, SocketFlags.None, new AsyncCallback(OnClientReceive), socket);
}
查看更多
登录 后发表回答