c# - 是否有现成的 .NET 方法来处理 HttpListener HttpListenerRequest 主体的响应主体?

标签 c# .net http

我正在使用 HttpListener 为本地主机上使用另一种技术编写的应用程序提供 Web 服务器。该应用程序使用简单的表单提交 (application/x-www-form-urlencoded) 向我的软件发出请求。我想知道是否已经编写了一个解析器来将 html 请求文档的主体转换为哈希表或等效项。

考虑到 .NET 似乎已经提供了这么多,我很难相信我需要自己写这个。

提前致谢

最佳答案

你的意思是像HttpUtility.ParseQueryString给你一个 NameValueCollection?这是一些示例代码。您需要更多的错误检查,并可能使用请求内容类型来确定编码:

string input = null;
using (StreamReader reader = new StreamReader (listenerRequest.InputStream)) {
    input = reader.ReadToEnd ();
}
NameValueCollection coll = HttpUtility.ParseQueryString (input);

如果您使用 HTTP GET 而不是 POST:

string input = listenerRequest.Url.QueryString;
NameValueCollection coll = HttpUtility.ParseQueryString (input);

关于c# - 是否有现成的 .NET 方法来处理 HttpListener HttpListenerRequest 主体的响应主体?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1767549/

相关文章:

security - 将 session 存储在加密的 cookie 中

c# - 无法调用 Invoke 或 BeginInvoke 错误

c# - 为什么会有 Nullable<T> 结构和 Nullable 类?

c# - TypeBuilder 的 Type.GetMethod 替代方案

c# - 错误 CS0246 找不到类型或命名空间名称 'JsonDeserializer' (RestSharp v107)

http - 如何在Go中正确使用http.ReadResponse?

java - servlet doGet 和 doPost 方法

c# - Url.Action() 导致 System.AccessViolationException

c# - "Control with id could not be located or a different control is assigned to the same ID after postback."错误

c# - 扩展方法可以应用于接口(interface)吗?