首页 > 解决方案 > 原始http请求解析

问题描述

我正在使用 windivert 来捕获 tcp 数据包。我成功捕获了 WinDivertSharp.dll 的数据包。现在我想解析只有http请求的数据包。要解析数据包,我正在使用流动代码。

var index = BinaryMatch(messageBody, Encoding.ASCII.GetBytes("\r\n\r\n")) + 4;
var headers = Encoding.ASCII.GetString(messageBody, 0, index);
var memory = new MemoryStream(messageBody);
memory.Position = index;
string body = "";
if (headers.IndexOf("Content-Encoding: gzip") > 0)
{
    using (GZipStream decompressionStream = new GZipStream(memory, CompressionMode.Decompress))
    using (var decompressedMemory = new MemoryStream())
    {
       decompressionStream.CopyTo(decompressedMemory);
       decompressedMemory.Position = 0;
       body = Encoding.UTF8.GetString(decompressedMemory.ToArray());
    }
 }
 else
 {
     body = Encoding.UTF8.GetString(messageBody, index, messageBody.Length - index);
 }    

它适用于某些网站。但是对于来自https://stackoverflow.com/的 http 请求,它不起作用。请帮助解码各种网络的 http 请求。

标签: c#httprequest

解决方案


可能是一个长镜头,但它可能与WinDivert不解密来自使用 SSL/TLS (HTTPS) 的站点的响应,例如https://stackoverflow.com/

我建议您尝试比较使用 HTTP 和 HTTPS 的站点。

希望能帮助到你!


推荐阅读