简体   繁体   中英

How to create a local-server that the web-browser control can use

For my newest windows-application I want to create an interface that is web-based using the web-browser control, and I want to make Ajax work on it. Since all the content will be loaded by the web-browser control itself I want to create a web-server that would work on a localhost port and serve my content to the web-browser when it accesses a special url, example:

http://localhost:3454

How can I create a web-server that windows will route requests to? I just need to know about the right API/WinInet commands to start receiving the requests.

Fortunate for you, .NET comes with a pretty decent Web Server built into the framework.

Look at the HttpListener class.

It supports both synchronous and asynchronous modes:

The synchronous model is appropriate if your application should block while waiting for a client request and if you want to process only one request at a time. Using the synchronous model, call the GetContext method, which waits for a client to send a request. The method returns an HttpListenerContext object to you for processing when one occurs.

In the more complex asynchronous model, your application does not block while waiting for requests and each request is processed in its own execution thread. Use the BeginGetContext method to specify an application-defined method to be called for each incoming request. Within that method, call the EndGetContext method to obtain the request, process it, and respond.

This guy is great because it uses the native web server that is built into modern versions of Windows (XP SP2+ and Server 2003+).

All you have to do is open a TCP socket , bind to port 3454 and listen for connections. Then you need to service each incoming connection by reading the stream, parsing the HTTP headers and content (if applicable).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM