【问题标题】:How can I use HttpListener when the url doesn't have ending by "/"?当 url 没有以“/”结尾时,如何使用 HttpListener?
【发布时间】:2015-06-28 03:49:28
【问题描述】:

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

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 没有以“/”结尾。如果我将它添加到 url 的末尾,则它无效并且不起作用。

那么我怎样才能收听一个不以“/”结尾的网址呢??

【问题讨论】:

  • 在 URL 中,? 之后分配的值是一个查询参数。例如https://www.google.co.in/search?q=xxxxxx,其中www.google.co.in/search 是一个URL,? 之后q=xxxxx 是一个查询参数。所以,如果你的 url 也包含这样的值,那么这意味着 url 包含查询参数。
  • 什么 URI 不起作用?
  • 你能发一个前缀的例子吗?

标签: c# httplistener


【解决方案1】:

您提供的 url 是一个前缀,因此前缀 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,但对于所有其他带有前缀的 URL,返回 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();
        }
    }
}

【讨论】:

    猜你喜欢
    • 2018-04-04
    • 2019-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-07
    • 1970-01-01
    • 2012-10-11
    • 2018-09-07
    相关资源
    最近更新 更多