当网址没有以" /"结尾时,如何使用HttpListener?

时间:2015-04-21 06:27:50

标签: c# httplistener

我想听一个获取一些信息的网址。所以我的代码是这样的:

public static void SimpleListenerExample(string[] prefixes)
{
    HttpListener listener = new HttpListener();
    // Add the prefixes. 
    foreach (string s in prefixes)
    {
        listener.Prefixes.Add(s);
    }
    listener.Start();
    //Console.WriteLine("Listening...");
    // Note: The GetContext method blocks while waiting for a request. 
    HttpListenerContext context = listener.GetContext();
    HttpListenerRequest request = context.Request;
    // Obtain a response object.
    HttpListenerResponse response = context.Response;
    // Construct a response. 
    string responseString = "<HTML><BODY> Hello world!</BODY></HTML>";
    byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString);
    // Get a response stream and write the response to it.
    response.ContentLength64 = buffer.Length;
    System.IO.Stream output = response.OutputStream;
    output.Write(buffer, 0, buffer.Length);
    // You must close the output stream.
    output.Close();
    listener.Stop();
}

private void button2_Click(object sender, EventArgs e)
{
    string[] test = { "http://xxx.xxxx.xxx.xxx:8086/sms.html" };
    SimpleListenerExample(test);
}

我想要添加为前缀的网址没有以“/”结尾。如果我在url的末尾添加它,它无效并且不起作用。

那么我怎么能听到一个没有以“/”结尾的网址?

1 个答案:

答案 0 :(得分:0)

您提供的网址是前缀,因此前缀http://xxx:8086的处理程序将匹配以该字符串开头的所有请求,包括http://xxx:8086/sms.html。这就是前缀必须以“/”结尾的原因,并在此处描述:https://msdn.microsoft.com/en-us/library/system.net.httplistener(v=vs.110).aspx

如果您希望侦听器只侦听特定路径,则必须编写一些逻辑,并检查请求中的URL。要按照请求执行此操作,您必须提供对httplistener的回调,并检查请求uri。

这是一个小程序,为http://localhost:8086/nisse.htmlhttp://localhost:8086/kalle.html返回200 OK,但对于带有前缀的所有其他网址则为404 http://localhost:8086/

class Program
{
    private static HttpListener listener;
    static string[] uris =
        {
            "http://localhost:8086/nisse.html",
            "http://localhost:8086/kalle.html",
        };

    private static void ListenerCallback(IAsyncResult result)
    {
        var context = listener.EndGetContext(result);

        HttpListenerRequest request = context.Request;            
        HttpListenerResponse response = context.Response;

        //Maybe not use exact string matching here
        if (uris.Contains(request.Url.ToString()))
        {                
            context.Response.StatusCode = 200;
            context.Response.StatusDescription = "OK";
            string responseString = "<HTML><BODY> YOU ASKED FOR:" + request.Url + "</BODY></HTML>";
            byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString);
            response.ContentLength64 = buffer.Length;
            System.IO.Stream output = response.OutputStream;
            output.Write(buffer, 0, buffer.Length);
            output.Close();
            context.Response.Close();
        }
        else
        {
            context.Response.StatusCode = 404;
            context.Response.StatusDescription = "NOT FOUND";
            context.Response.Close();
        }
    }

    private static void Main()
    {                 
        listener = new HttpListener();  
        //Add the distinct prefixes, you may want ot parse this in a more elegant way
        foreach (string s in uris.Select(u=>u.Substring(0,u.LastIndexOf("/")+1)).Distinct())
        {
            listener.Prefixes.Add(s);
        }
        listener.Start();
        while (true)
        {
            var result = listener.BeginGetContext(ListenerCallback, listener);
            result.AsyncWaitHandle.WaitOne();
        }
    }
}