1
我有一個簡單的Web API,它將檢索圖像文件並將字節返回給調用者。當我在本地(IIS Express)中託管Visual Studio中的api項目時,它會工作,但是,當我將這個項目發佈到我的IIS服務器時,它會失敗。任何人有任何想法?IIS服務器上的Web API內的Httpclient GetAsync失敗(503服務不可用)
[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()' –
請不要刪除堆棧跟蹤...堆棧跟蹤指向您的代碼行數... – Hackerman
@馬蒂亞斯西塞羅,我同意,我會改變代碼。 –