IIS服务器上的Web API内的Httpclient GetAsync失败(503 Service Unavailable)

时间:2016-09-27 12:47:44

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

我有一个简单的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>

1 个答案:

答案 0 :(得分:0)

转到项目属性。

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

enter image description here