[英]Why my server can not respond to the client's request from the webbrowser?
[英]Why can my WebApi client make only a single GET request?
如本教程所述,我為使用Web Api的Web服務編寫了一個簡單的客戶端:
http://www.asp.net/web-api/overview/web-api-clients/calling-a-web-api-from-a-net-client
第一個請求(獲取表)已成功執行。 也就是說,表數據是從Web服務中獲取的,並綁定到TableContent對象。
但是在執行第二個請求(GET表列表)之前,程序被完全阻塞了; 沒有任何反應,甚至沒有錯誤消息!
如果我注釋掉與第一個請求(獲取表)相關的代碼段,則第二個請求(獲取表列表)將成功執行。
這里會發生什么? 為什么此客戶端只能執行一個GET請求?
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("http://localhost:56510/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response;
// GET a table from web service
response = await client.GetAsync("api/table/GetMatrixTable");
if (response.IsSuccessStatusCode)
{
var tblcont = await response.Content.ReadAsAsync<TableContent>();
// do things ...
}
// GET a table list from web service
response = await client.GetAsync("api/table/GetTableList");
if (response.IsSuccessStatusCode)
{
var TblContList = await response.Content.ReadAsAsync<IList<TableContent>>();
// do things ...
}
}
查看下面的SO問題及其接受的答案(可能不直接相關,但就其自身而言仍然有用):
如果您按以下方式替換對GetAsync()
調用,則可能會有所幫助:
response = await client.GetAsync("api/table/GetMatrixTable",
HttpCompletionOption.ResponseHeadersRead).ConfigureAwait(false);
. . .
response = await client.GetAsync("api/table/GetTableList",
HttpCompletionOption.ResponseHeadersRead).ConfigureAwait(false);
我無法對此進行測試,因此如果可行的話,我將無法承擔全部責任。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.