【问题标题】:Httpclient GetAsync Fails(503 Service Unavailable) inside Web API on IIS ServerIIS 服务器上的 Web API 内的 Httpclient GetAsync 失败(503 服务不可用)
【发布时间】:2016-09-27 12:47:44
【问题描述】:

我有一个简单的 web api,它将检索图像文件并将字节返回给调用者。当我在本地(IIS Express)的visualstudio中托管api项目时它会起作用,但是当我将此项目发布到我的IIS服务器时它会失败。有人有什么想法吗?

[RoutePrefix("api/v1/Test")]
public class TestController : ApiController
{
    [Route("GetBytesCount")]
    public async Task<int> GetBytesCount()
    {
        var client = new HttpClient() { Timeout = new TimeSpan(0, 5, 0) };
        client.DefaultRequestHeaders.Accept.Clear();
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

        var response = await client.GetAsync(@"http://icons.wxug.com/i/c/k/clear.gif");
        response.EnsureSuccessStatusCode();
        var imageBytes = await response.Content.ReadAsByteArrayAsync();
        return imageBytes.Length;
    }

}

如果我从本地盒子里调用它

http://localhost:57988/lkr/api/v1/Test/Getbytescount

它会返回

int xmlns="http://schemas.microsoft.com/2003/10/Serialization/">1689</int>

当我从我的服务器调用它时,它会返回

<Error>
<Message>An error has occurred.</Message>
<ExceptionMessage>
Response status code does not indicate success: 503 (Service Unavailable).
</ExceptionMessage>
<ExceptionType>System.Net.Http.HttpRequestException</ExceptionType>
<StackTrace>
......
</StackTrace>
</Error>

【问题讨论】:

  • 作为不相关的旁注,您应该使用response.Content.ReadAsByteArrayAsync()
  • 请不要删除堆栈跟踪...堆栈跟踪指向您代码中的行号......
  • @Matias Cicero,我同意,我会更改代码。
  • 服务器的EventLog中有条目吗?
  • @Hackerman 这是行“response.EnsureSuccessStatusCode();”抛出异常。 http状态码是503。

标签: c# asp.net-web-api httpclient getasync


【解决方案1】:

转到项目属性。

获取 SSL URL 并在浏览器中使用以访问资源。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-16
    • 2011-07-22
    • 2015-06-10
    • 2012-12-18
    • 2021-03-12
    相关资源
    最近更新 更多