2012-07-09 79 views
0

这是我的监控HTTP代码:HTTP监视器在C#

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主机。 我不知道什么数据。 我想获取http主机,例如: http://google.com 我该如何监控系统http? 谢谢。

+0

这是我的数据是:http://hamishebaharp30world.persiangig.com/Pics/Use/http%20monitoring .jpg – 2012-07-09 09:29:17

+0

你试过解压吗?可以在服务器端进行压缩(通过头文件告知),然后你会看到垃圾。无论如何,我不确定您是否可以区分不同的请求,但正如我所说,我不确定这是否会成为问题。 – Sascha 2012-07-09 09:57:43

+0

你是如何摆弄软件的?我想让浏览器成为事实。 – 2012-07-09 10:42:10

回答

0

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

PS:见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); 
} 
+0

我试图创建一个新的控制台应用程序使用这段代码,似乎没有任何事情发生。 – michael 2012-07-23 01:58:22