當我在服務器上發送資料并且服務器沒有啟動時,我收到一個例外 Unable to connect to the server 并且 UI 視窗實體在執行代碼行時關閉: (response = await client.PostAsJsonAsync("windows /actions", data).ConfigureAwait(false);)。如何停止不應關閉的 UI 視窗。
我的代碼:
public static async void PostInfo(List<ElementProps> requestObj)
{
try
{
HttpResponseMessage response;
using (HttpClient client = new HttpClient())
{
// Setting Base address.
client.BaseAddress = new Uri("http://126.1.1.1:8888/");
// Setting content type.
client.DefaultRequestHeaders.Accept.Add(new
MediaTypeWithQualityHeaderValue("application/json"));
string list = JsonConvert.SerializeObject(requestObj);
object data = JsonConvert.DeserializeObject(list);
// HTTP POST ** Here is the error **
response = await client.PostAsJsonAsync("windows/actions", data).ConfigureAwait(false);
// Verification
if (response.IsSuccessStatusCode)
{
System.Windows.MessageBox.Show("Recording saved successfully!"); <br/>
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
ErrorLog.Log(ex);
}
}
uj5u.com熱心網友回復:
我的主執行緒在 http 客戶端異步操作完成之前退出。
我只是添加.Wait()到呼叫者來維護主執行緒。所以,我有這個:
PostInfo(Records).Wait();
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/372646.html
標籤:C# http xmlhttp请求 dotnet-httpclient
