C# HttpListener“错误请求”问题

发布于 2024-10-15 16:43:18 字数 2317 浏览 2 评论 0原文

由于某种原因,当请求通过非标准地址发送到 HttpListener 时,它会返回

<h1>Bad Request (Invalid Hostname)</h1>

示例数据包:

GET /index HTTP/1.1
Host: ::ffff:88.118.32.126:2548
Accept: */*

HTTP/1.1 400 Bad Request
Content-Type: text/html
Server: Microsoft-HTTPAPI/1.0
Date: Wed, 02 Feb 2011 19:05:18 GMT
Connection: close
Content-Length: 39

<h1>Bad Request (Invalid Hostname)</h1>

我该如何解决这个问题?

编辑: 这是我的代码

public class Server
{
    private HttpListener Listener;
    private Log Log;

    public int Port = 1555;

    public Server(Log _log)
    {
        Log = _log;
    }

    public void Start()
    {
        Listener = new HttpListener();
        Listener.Prefixes.Add(string.Format("http://{0}:{1}/",
            "88.118.32.126", Port));

        Listener.Prefixes.Add(
            string.Format("http://{0}:{1}/",
            "*", Port)); //added these for testing, but still nothing

        Listener.Prefixes.Add(
            string.Format("http://::ffff:{0}:{1}/",
            "*", Port)); //added these for testing, but still nothing

        Listener.Start();
        Listener.BeginGetContext(ProcessRequest, Listener);

        Log.Add(string.Format("HTTP Server started on port {0}",
            Port), LogType.Info, true);
    }

    private void ProcessRequest(IAsyncResult result)
    {
        HttpListener listener = (HttpListener)result.AsyncState;
        HttpListenerContext context = listener.EndGetContext(result);

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

        string[] UrlParts = request.RawUrl.Split('/');
        Response output;

        if (request.RawUrl == "/index")
            output = new Response(context.Response.OutputStream, 
                ResponseType.Stats, UrlParts, Log);

        response.ContentLength64 = output.Size;
        response.ContentType = output.Header;

        output.Send();

        if (result.IsCompleted)
            Log.Add(String.Format("OUT > {1,15} {0,8} bytes -> {2}",
                output.Size, request.RemoteEndPoint.Address,
                request.RawUrl), LogType.Info, true);

        Listener.BeginGetContext(ProcessRequest, Listener);
    }
}

For some reason when request is sent to HttpListener via non-standart address it returns

<h1>Bad Request (Invalid Hostname)</h1>

Example packet:

GET /index HTTP/1.1
Host: ::ffff:88.118.32.126:2548
Accept: */*

HTTP/1.1 400 Bad Request
Content-Type: text/html
Server: Microsoft-HTTPAPI/1.0
Date: Wed, 02 Feb 2011 19:05:18 GMT
Connection: close
Content-Length: 39

<h1>Bad Request (Invalid Hostname)</h1>

How could i solve this?

Edit:
This is my code

public class Server
{
    private HttpListener Listener;
    private Log Log;

    public int Port = 1555;

    public Server(Log _log)
    {
        Log = _log;
    }

    public void Start()
    {
        Listener = new HttpListener();
        Listener.Prefixes.Add(string.Format("http://{0}:{1}/",
            "88.118.32.126", Port));

        Listener.Prefixes.Add(
            string.Format("http://{0}:{1}/",
            "*", Port)); //added these for testing, but still nothing

        Listener.Prefixes.Add(
            string.Format("http://::ffff:{0}:{1}/",
            "*", Port)); //added these for testing, but still nothing

        Listener.Start();
        Listener.BeginGetContext(ProcessRequest, Listener);

        Log.Add(string.Format("HTTP Server started on port {0}",
            Port), LogType.Info, true);
    }

    private void ProcessRequest(IAsyncResult result)
    {
        HttpListener listener = (HttpListener)result.AsyncState;
        HttpListenerContext context = listener.EndGetContext(result);

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

        string[] UrlParts = request.RawUrl.Split('/');
        Response output;

        if (request.RawUrl == "/index")
            output = new Response(context.Response.OutputStream, 
                ResponseType.Stats, UrlParts, Log);

        response.ContentLength64 = output.Size;
        response.ContentType = output.Header;

        output.Send();

        if (result.IsCompleted)
            Log.Add(String.Format("OUT > {1,15} {0,8} bytes -> {2}",
                output.Size, request.RemoteEndPoint.Address,
                request.RawUrl), LogType.Info, true);

        Listener.BeginGetContext(ProcessRequest, Listener);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浪荡不羁 2024-10-22 16:43:18

发生此错误的一种原因是 HTTP.SYS 无法将 HTTP 主机标头与其有效 UrlAcl 列表相匹配。这可能是由于解析主机名时出现问题。

  1. 解释配置步骤的相关答案
  2. 有关设置 HTTP 独立的 MSDN 文档
  3. 修复“错误请求(无效主机名)” ” - 400 Error, on Windows IIS Server(相关潜在解决方案和评论)

您可以在命令行列出映射:

Windows XP 支持工具 / 2003 支持工具 - httpcfg

httpcfg query urlacl

包含在 Windows Vista / 2008 / 7+ 中 - <代码>netsh

netsh http show urlacl

One way this error occurs is when HTTP.SYS can not match the HTTP Host header with its list of valid UrlAcl's. This could be because of problems resolving a hostname.

  1. Related answer explaining configuration steps
  2. MSDN documentation about setting up HTTP stand-alone
  3. Fixing a “Bad Request (Invalid Hostname)” - 400 Error, on Windows IIS Server (relevant potential solutions and commentary)

You can list the mappings at the command line:

Windows XP Support Tools / 2003 Support Tools - httpcfg

httpcfg query urlacl

Included with Windows Vista / 2008 / 7+ - netsh

netsh http show urlacl
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文