Search code examples
c#httptcppcap.net

How can I create HttpResponseDatagram and HttpResponseLayer in pcap.net


I cant create these objects in pcap.net

IpV4Datagram ip = packet.Ethernet.IpV4;
        TcpDatagram tcp = ip.Tcp;
        HttpDatagram http = tcp.Http;
     //   HttpResponseDatagram resdat=http.;  How can i create them?
     //   HttpResponseLayer resp;
if (tcp.Http.Body != null && http.IsResponse)
            body = resp.Body.ToString();

I'm trying to get HTTP Response Body from a tcp packet. If there is another way to do it can someone help?


Solution

  • In the Pcap.Net User Guide, there's a section about sending packets. In that section, there is an example on how to create HttpRequestLayer, and creating HttpResponseLayer is very similar.

     HttpRequestLayer httpLayer =
         new HttpRequestLayer
         {
             Version = HttpVersion.Version11,
             Header = new HttpHeader(new HttpContentLengthField(11)),
             Body = new Datagram(Encoding.ASCII.GetBytes("hello world")),
             Method = new HttpRequestMethod(HttpRequestKnownMethod.Get),
             Uri = @"http://pcapdot.net/",
         };
    

    HttpResponseDatagram is creating by parsing an HTTP packet. For example, by doing

    packet.Ethernet.IpV4.Tcp.Http
    

    There's no simple way of creating a Datagram without parsing a packet.