3

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?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Zoszko
  • 75
  • 1
  • 7

3 Answers3

4

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

Jan Köhler
  • 5,817
  • 5
  • 26
  • 35
  • Well, this OWIN / Katana project looks promising. I can confirm that what @Shahrooz linked in the other answer works, and I am sure this one does as well. But I am not closer to get those parameters. Could you please be more 'specific'? – Zoszko Feb 01 '15 at 17:14
  • See my updated answer. Further research is up to you ;) – Jan Köhler Feb 01 '15 at 18:09
4

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();
        }
    }
  • Could you please be a little more specific on how to implement this into my app? – Zoszko Feb 01 '15 at 17:32
  • @Zoszko you can go to this article: http://odetocode.com/blogs/scott/archive/2014/02/10/building-a-simple-file-server-with-owin-and-katana.aspx –  Feb 01 '15 at 18:05
0

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;
            }
Rahul Chawla
  • 1,048
  • 10
  • 15
Cosmin
  • 1