我跟隨this example,它在一個控制檯應用程序,但後來我嘗試了一個Windows窗體應用程序,它在結果是await client.GetAsync("api/branches/1035")
它是如何不同?http客戶端凍結在Windows窗體應用
控制檯代碼(這個工程):
static void Main()
{
RunAsync().Wait();
}
static async Task RunAsync()
{
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("http://localhost:49358/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = await client.GetAsync("api/branches/1035");
if (response.IsSuccessStatusCode)
{
branch branch = await response.Content.ReadAsAsync<branch>();
Console.WriteLine("{0}\t${1}", branch.Id, branch.Color);
}
}
}
,這是冰凍的,當它擊中await client.GetAsync("api/branches/1035")
private void button1_Click(object sender, EventArgs e)
{
RunAsync().Wait();
}
static async Task RunAsync()
{
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("http://localhost:49358/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = await client.GetAsync("api/branches/1035");
if (response.IsSuccessStatusCode)
{
Branch branch = await response.Content.ReadAsAsync<Branch>();
Console.WriteLine("{0}\t${1}", branch.Id, branch.Color);
}
}
}
爲什麼你的button1_Click仍在試圖寫入控制檯?它不再是一個控制檯應用程序。 –
@KenWhite是我的壞,忘了那條線,我試圖從我的api得到一些「分支」,然後客戶端應該返回他喜歡的那些 –