我正在使用 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/