【问题标题】:What causes a HttpListener HTTP 503 error?是什么导致 HttpListener HTTP 503 错误?
【发布时间】:2011-12-29 20:30:27
【问题描述】:

所以,我们有一个大型程序,它使用 HttpListener 来实现一个小的远程管理功能。由于我不明白的原因,有些人遇到了 503 错误的问题。

由于我们没有提供错误,因此框架中发生了一些事情。现在,我的问题是,框架内部是什么提供了这个错误?是前缀设置不正确还是怎么的?

我们目前将前缀设置为“http://*:8080/”。

建议?

【问题讨论】:

  • 它是否有时会工作而不会出现错误 503?
  • 我不确定。这是一个有麻烦的客户。如果它是/不是恒定的,有什么建议吗?
  • 如果不是恒定的,我听起来不像是权限问题...

标签: c# httplistener


【解决方案1】:

在尝试使用 netsh http 命令为 HttpListener 设置权限时,我在 Windows 7 上遇到了同样的错误。 在目标系统上运行命令(适用于 Windows 7):

netsh http show urlacl    

并检查您的网址“http://+:8080/”是否已出现在保留网址列表中。尝试从列表中删除(使用“netsh http delete urlacl”。类似主题here

【讨论】:

  • 很好的答案,我遇到了同样的问题:我的一个测试使用了 HttpSelfHostConfiguration 类,该类需要保留它使用的 url(通过 netsh),而另一个使用 HttpListener 的测试在我抛出 HTTP 503 错误时想要访问它,因为该 url 是在第一次测试之前保留的。
  • 我在尝试访问在自定义端口上运行的 HttpListener 时遇到了同样的问题(503 错误)。我在运行show urlacl 后找到了该URL,并使用delete urlacl [custom URL:port] 将其删除。之后它起作用了。谢谢。
  • 它可以工作,但是为什么在关闭我使用该 URL 的应用程序后,即使在重新启动 Windows 后,该 URL 仍然保留?
  • 因为保留的 URL 列表是系统范围的,不依赖于任何进程的运行状态。
  • 是的,我这样做:【netsh http add urlacl url=http://*:8080/ user=everyone listen=yes】和【netsh http delete urlacl url=http://+:8080/】,它对我有用!
【解决方案2】:

这个问题我有点晚了。但是我只是在以下情况下遇到了HTTP 503状态码:

例如,如果使用 netsh http add urlacl 注册了“http://+:8080/MyService/http://+:8080/MyService/SOAP”,我们会收到针对不明确 URL 的请求的 503。对“http://myservice:8080/MyService/somethingelse”的请求工作正常,而对“http://myservice:8080/MyService/SOAP/”的请求失败,状态为 503。

为了完整起见,添加了这个。我花了一段时间才弄清楚。

【讨论】:

    【解决方案3】:

    确保您的 HttpListenerurlacl 匹配。

    以 (C#) 方式启动您的侦听器:

    using System;
    using System.Net;
    using System.ServiceModel;
    
    namespace Example
    {
        class MyApi
        {
            private const int _apiPortNumber = 8080;
            public const string BasePath = "/myApi/";
            public static EndpointAddress MyEndPoint => new EndpointAddress(
                new UriBuilder(
                        Uri.UriSchemeHttp,
                        "localhost",//only allow connections on localhost (no remote access)
                        _apiPortNumber,
                        BasePath)
                    .Uri);
    
            public MyApi()
            {
                var httpListener = new System.Net.HttpListener();
                httpListener.Prefixes.Add(MyEndPoint.Uri.ToString());
                httpListener.Start();
            }
        }
    }
    

    urlacl 的作用是:

    netsh http add urlacl url=http://127.0.0.1:8080/myApi/ user=MyServiceUserName

    但是,如下配置您的 urlacl 将不起作用:

    http add urlacl url=http://+:8080/myApi/ user=MyServiceUserName

    【讨论】:

      猜你喜欢
      • 2018-07-13
      • 2014-12-12
      • 1970-01-01
      • 2021-01-20
      • 1970-01-01
      • 2020-09-15
      • 2021-09-06
      • 2015-03-04
      相关资源
      最近更新 更多