这是我的监控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主机。 我不知道有什么数据。 我想获得http主机例如: http://google.com 我怎么能监控系统http? 感谢。
答案 0 :(得分:0)
您在链接中看到的是IP + TCP headers。您应该解析IP和TCP标头以提取内容。 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);
}