是否有一个.NET ready方法来处理HttpListener HttpListenerRequest主体的响应主体?

Jot*_*ham 15 .net c# http

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

考虑到.NET已经提供了多少.NET,我发现自己很难相信我需要自己写这个.

提前致谢,

Gon*_*alo 25

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

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

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

string input = listenerRequest.Url.QueryString;
NameValueCollection coll = HttpUtility.ParseQueryString (input);
Run Code Online (Sandbox Code Playgroud)

  • @nitzmahone:真的吗?阅读http://www.w3.org/TR/html401/interact/forms.html#h-17.13.1.x-www-form-urlencoded的POST中的数据与等效GET的查询strinng相同. (2认同)