Home > Back-end >  How can I parse a raw HTTP response in .net?
How can I parse a raw HTTP response in .net?

Time:04-28

I've got a project which deals with byte streams, and some of them are HTTP responses. I need to parse a raw HTTP response from this byte stream.

I can do this myself, but there must already be code that exists to do this, so I started looking at Kestrel, but I can't see any way to do what I'm attempting. Perhaps it's overkill for my needs any way.

How can I parse a raw HTTP response in .net?

Here's an example, decoded to ASCII:

HTTP/1.1 200 OK
Content-Type: text/html
Last-Modified: Sun, 17 Apr 2022 03:32:06 GMT
Accept-Ranges: bytes
ETag: "374b9ab6b52d81:0"
Server: Microsoft-IIS/10.0
X-Powered-By: ASP.NET
Date: Wed, 27 Apr 2022 10:45:49 GMT
Content-Length: 696

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>IIS Windows</title>
<style type="text/css">
<!--
body {
    color:#000000;
    background-color:#0072C6;
    margin:0;
}

#container {
    margin-left:auto;
    margin-right:auto;
    text-align:center;
    }

a img {
    border:none;
}

-->
</style>
</head>
<body>
<div id="container">
<a href="http://go.microsoft.com/fwlink/?linkid=66138&amp;clcid=0x409"><img src="iisstart.png" alt="IIS" width="960" height="600" /></a>
</div>
</body>
</html>

CodePudding user response:

HttpWebResponse implements ISerializable.

So you could simply deserialize your byte stream using a BinaryFormatter

var formatter = new BinaryFormatter();
var webResponse = (HttpWebResponse)formatter.Deserialize(sourceByteStream);

CodePudding user response:

I ended up using this Nuget package:

https://www.nuget.org/packages/HttpMachine.PCL/4.0.3

  • Related