C#中的Http Monitor [英] Http Monitor in C#

查看:99
本文介绍了C#中的Http Monitor的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我监控Http的代码:

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);
     }
}

但我无法获得http主机。
我不知道有什么数据。
i希望获得http主机,例如:
http://google.com
我怎么能监控系统http?
谢谢。

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.

推荐答案

您在链接中看到的是 IP + TCP标头。您应该解析IP和TCP标头以提取内容。 TCP内容在偏移量40处开始大约。因此,您可以尝试以下程序的修改版本,以查看每个 HTTP 请求的内容。 (工作但不是完整的程序只是为了给你一个想法)

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:参见 s.Bind(新的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);
}

这篇关于C#中的Http Monitor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆