title: SharpPcap - Getting information from packet
body: <p>I'm trying to get information from packets, like GET/POST requests using SharpPcap and PacketDotNet. I looked different information here on stackoverflow, but I have encoding problems. Information from packets isn't shown properly in every taken Encoding system.</p>
  
  <p>My code below:</p>
  
  <pre><code>using System.Text;
  using System.Threading;
  using PacketDotNet;
  using SharpPcap;
  using System;
  
  namespace ConsoleApplication2
  {
      class Program
      {
          static void Main(string[] args)
          {
              string ver = SharpPcap.Version.VersionString;
              Console.WriteLine("SharpPcap {0}", ver);
  
              // Retrieve the device list
              CaptureDeviceList devices = CaptureDeviceList.Instance;
  
              ICaptureDevice myDevice = devices[3];
              myDevice.OnPacketArrival += new PacketArrivalEventHandler(device_OnPacketArrival);
              myDevice.Open(DeviceMode.Promiscuous, 1000);
              //Console.WriteLine("trst = {0}", myDevice.MacAddress);
              //myDevice.Filter = "tcp and dst host 10.11.16.80";
  
  
              Console.WriteLine("-- Listening on {0}, hit 'Enter' to stop...",
                            myDevice.Description);
              myDevice.StartCapture();
              Console.ReadLine();
              myDevice.StopCapture();
              myDevice.Close();
  
          }
  
          private static void device_OnPacketArrival(object sender, CaptureEventArgs e)
          {
          // парсинг всего пакета
              Packet packet = Packet.ParsePacket(e.Packet.LinkLayerType, e.Packet.Data);
          // получение только TCP пакета из всего фрейма
              var tcpPacket = TcpPacket.GetEncapsulated(packet);
          // получение только IP пакета из всего фрейма
              var ipPacket = IpPacket.GetEncapsulated(packet);
              if (tcpPacket != null &amp;&amp; ipPacket != null)
              {
                  DateTime time = e.Packet.Timeval.Date;
                  int len = e.Packet.Data.Length;
  
              // IP адрес отправителя
                  var srcIp = ipPacket.SourceAddress.ToString();
              // IP адрес получателя
                  var dstIp = ipPacket.DestinationAddress.ToString();
  
              // порт отправителя
                  var srcPort = tcpPacket.SourcePort.ToString();
              // порт получателя
                  var dstPort = tcpPacket.DestinationPort.ToString();
              // данные пакета
                  var hex = BitConverter.ToString(tcpPacket.PayloadData);
  
                  hex = hex.Replace("-", "");
                  byte[] raw = new byte[hex.Length / 2];
                  for (int i = 0; i &lt; raw.Length; i++)
                  {
                      raw[i] = Convert.ToByte(hex.Substring(i * 2, 2), 16);
                  }
  
                  string data = Encoding.ASCII.GetString(raw);
  
                  Console.WriteLine("{0}:{1}:{2}",srcPort,dstPort, data);
                  Thread.Sleep(1000);
              }
          }
      }
  }
  </code></pre>
  
  <p>Result are shown on screenshot: <a href="http://i.stack.imgur.com/laDed.jpg" rel="nofollow">Screenshot</a></p>