我有一個處理位元組流的專案,其中一些是 HTTP 回應。我需要從這個位元組流中決議一個原始的 HTTP 回應。
我自己可以做到這一點,但必須已經有代碼可以做到這一點,所以我開始查看Kestrel,但我看不到任何方法可以做我正在嘗試的事情。也許這對我的需求來說有點過頭了。
如何在 .net 中決議原始 HTTP 回應?
這是一個示例,解碼為 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&clcid=0x409"><img src="iisstart.png" alt="IIS" width="960" height="600" /></a>
</div>
</body>
</html>
uj5u.com熱心網友回復:
HttpWebResponse 實作 ISerializable。
所以你可以簡單地使用 BinaryFormatter 反序列化你的位元組流
var formatter = new BinaryFormatter();
var webResponse = (HttpWebResponse)formatter.Deserialize(sourceByteStream);
uj5u.com熱心網友回復:
我最終使用了這個 Nuget 包:
https://www.nuget.org/packages/HttpMachine.PCL/4.0.3
using (var handler = new HttpParserDelegate())
using (var parser = new HttpCombinedParser(handler))
{
bArray = TestReponse();
Console.WriteLine(parser.Execute(bArray) == bArray.Length
? $"Reponse test succeed. Type identified is; {handler.HttpRequestResponse.MessageType} \r\n"
$"Headers: \r\n"
$"{string.Join("\r\n", handler.HttpRequestResponse.Headers.Select(h => $"{h.Key}: {string.Join(", ", h.Value)} "))}"
: $"Response test failed");
handler.HttpRequestResponse.Body.Position = 0;
var reader = new StreamReader(handler.HttpRequestResponse.Body);
var body = reader.ReadToEnd();
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/471504.html
上一篇:ShinyServer對瀏覽器URL做出反應,但對來自httr的GET沒有反應
下一篇:如何從json中獲取“@microsoft.graph.downloadUrl”,因為@是關鍵字,不能用于變數。@microsoft.graph.downloadUrl無法宣告
