c#中的Http库

HS.*_*HS. 3 c# http

我正在将一个小型Web服务器嵌入到我的程序中.它相对简单 - 只需要提供原始的html文件和javascript.

我有一些异步网络代码,我可以使用它来获得基本的管道.但是有没有现成的库可以理解http?

我只需要能够解析http请求以提取路径,查询字符串,发布变量并能够相应地做出响应.

无需SSL或cookie或身份验证.

我尝试了几个Web服务器库,但不满意,主要是因为他们使用工作线程与程序的UI交互烦人.

理想情况下,我只想要一个带有一些http请求字符串\ stream的库,然后给我一个结构或对象.

Eri*_*bal 7

我认为HttpListener可能会做你想要的.

编辑:(添加代码示例以防万一)

这里有一些代码来展示如何使用它(使用异步方法).

HttpListener _server = new HttpListener();

// add server prefix (this is just one sample)
_server.Prefixes.Add("http://*:8080");

// start listening
_server.Start();

// kick off the listening thread
_Server.BeginGetContext(new AsyncCallback(ContextCallback), null);
Run Code Online (Sandbox Code Playgroud)

然后在 ContextCallback(IAsyncResult result)

// get the next request
HttpListenerContext context = _server.EndGetContext(result);

// write this method to inspect the context object
// and do whatever logic you need
HandleListenerContext(context);

// is the server is still running, wait for the next request
if (_Server.IsListening)
{
    _server.BeginGetContext(new AsyncCallback(ServerThread), null);
}
Run Code Online (Sandbox Code Playgroud)

看一下HttpListenerContext,了解您可以使用的内容,但主要内容可能是Request属性.