Http library in C #

I am in the process of implementing a small web server in my program. It is relatively simple - you only need to serve raw html files and javascript.

I have some asynchronous network code that I can use to get basic plumbing. But are there any libraries available that can understand http?

I just need to parse the HTTP request in order to extract the path, request string, mail variables and respond accordingly.

No SSL or cookies or authentication required.

I tried a couple of web server libraries, but was not satisfied, mainly because they use workflows, which make the interaction with the user interface of the program annoying.

Ideally, I just want a library that would require some http \ stream query string and return me a structure or object.

+3
source share
2 answers

I think the HttpListener can do what you want.

EDIT: (added code example just in case)

Here is some code to show how you can use it (using async methods).

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);

and then in 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);
}

Take a look at the HttpListenerContext for details of what you have for you, but the main one is likely to be Request.

+6
source

Why aren't you using classes from the namespace System.Net? Especially the HttpListener .

+2
source

Source: https://habr.com/ru/post/1706747/


All Articles