简体   繁体   中英

C# Simple web server with url parameters

I am trying to create little program which is going to act like a web server and accepts url parameters. I have found this example project: https://codehosting.net/blog/BlogEngine/post/Simple-C-Web-Server.aspx

Is there any way to make this receive my url parameters? Is there any other example project like this which has this functionality?

You should take a look at OWIN/Katana. Based on your question – and with OWIN's ability to be hosted in any process – this might fit quite well and is the current way to go:

http://www.asp.net/aspnet/overview/owin-and-katana/getting-started-with-owin-and-katana

You'll find tons of sample on this topic. For your question related to parameters you could refer to this article .

Based on the first link you could do something like the follwing:

public class Startup1
{
    public void Configuration(IAppBuilder app)
    {
        app.Run(context =>
        {
            var value = context.Request.Query.Get("someKey");

            if (value == "foo")
            {
                // do something
            }

            context.Response.ContentType = "text/plain";
            return context.Response.WriteAsync("Hello, world.");
        });
    }
}

A request could look like this: http://someServer:80/?someKey=foo

Please read this article: Building A Simple File Server With OWIN and Katana

class Program
    {
        static void Main(string[] args)
        {
            var url = "http://localhost:8080";
            WebApp.Start(url, builder => builder.UseFileServer(enableDirectoryBrowsing:true));            
            Console.WriteLine("Listening at " + url);
            Console.ReadLine();
        }
    }

Based on your project example try to put in method SendRequest as follow:

string name = request.QueryString.Get("name");
            switch (name){
                case "a":
                    return string.Format("<HTML><BODY>My web page a.<br>{0}</BODY></HTML>", DateTime.Now);
                    break;
                case "b":
                    return string.Format("<HTML><BODY>My web page b.<br>{0}</BODY></HTML>", DateTime.Now);
                    break;
                default:
                    return string.Format("<HTML><BODY>My web page .<br>{0}</BODY></HTML>", DateTime.Now);
                    break;
            }

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