HttpListener为具有尾随点的主机返回400 Bad Request

时间:2015-02-16 15:13:59

标签: .net httplistener

看起来HttpListener类返回状态400"错误请求"如果Host标头包含带有尾随点的主机名。

代码(基于HttpListener example):

using System.Text;
using System.Net;

namespace HttpListenerTest
{
    class Program
    {
        static void Main(string[] args)
        {
            SimpleListenerExample(new []{"http://*:8081/"});
        }

        public static void SimpleListenerExample(string[] prefixes)
        {
            var listener = new HttpListener();
            foreach (var s in prefixes)
                listener.Prefixes.Add(s);
            listener.Start();
            while (true)
            {
                var context = listener.GetContext();
                var response = context.Response;
                var buffer = Encoding.UTF8.GetBytes("<HTML><BODY> Hello world!</BODY></HTML>");
                response.ContentLength64 = buffer.Length;
                var output = response.OutputStream;
                output.Write(buffer, 0, buffer.Length);
                output.Close();
            }
        }
    }
}

然后运行curl:

Error 400:
c:\...> curl -v --header "Host: localhost.local." http://localhost.:17320/
c:\...> curl -v --header "Host: localhost." http://localhost.:17320/

OK 200:
c:\...> curl -v --header "Host: localhost.local" http://localhost.:17320/
c:\...> curl -v --header "Host: localhost" http://localhost.:17320/

任何方式配置HttpListener的方式都是&#34; localhost&#34;和#34; localhost。&#34;请求同样适用?

0 个答案:

没有答案