HttpClient.PostAsync 崩溃应用。如何捕捉异常?
Posted
技术标签:
【中文标题】HttpClient.PostAsync 崩溃应用。如何捕捉异常?【英文标题】:HttpClient.PostAsync crash app. How to catch the Exception? 【发布时间】:2020-12-26 12:39:22 【问题描述】:我正在尝试测试这个 HttpRequest:
public void TestX(string baseUrl)
StringContent httpContentDistanza = new StringContent(GlobalVariables.JsonDistanza);
using HttpClient httpClient = new HttpClient
BaseAddress = new Uri(baseUrl)
;
HttpResponseMessage responseMessage = null;
try
responseMessage = httpClient.PostAsync("/xxx/xx/xxx", httpContentDistanza).Result;
// can't reach the code below
if (responseMessage.IsSuccessStatusCode)
string strContext = responseMessage.Content.ReadAsStringAsync().Result;
var risultato = JsonSerializer.Deserialize<Distanza1>(strContext);
GlobalVariables.DblAijCrnPsz = risultato.data.processDataIn.valore;
catch (Exception ex)
if (responseMessage == null)
responseMessage = new HttpResponseMessage();
responseMessage.StatusCode = HttpStatusCode.InternalServerError;
responseMessage.ReasonPhrase = string.Format("RestHttpClient.SendRequest failed: 0", ex);
问题是 URI 不可访问,我原以为它会抛出一些异常,但它没有。
如果 URI 无法访问,我需要一些方法来捕获该异常。
我正在使用BackgroundWorker
运行TestX()
:
public MainWindow()
InitializeComponent();
bgWorker = new BackgroundWorker WorkerReportsProgress = true ;
bgWorker.DoWork += ResetAll;
private void ResetAll(object sender, DoWorkEventArgs e)
var x = gestLink.TestX(GlobalVariables.BaseUrl).ToString();
//it also does't reach the code below
....
更新
我不知道我做错了什么..我仍然无法捕捉到异常:
public async Task TestX(string baseUrl)
StringContent httpContentDistanza = new StringContent(GlobalVariables.JsonDistanza);
using HttpClient httpClient = new HttpClient
BaseAddress = new Uri(baseUrl)
;
HttpResponseMessage responseMessage = null;
try
responseMessage = await client.PostAsync("/xxx/xxx/xx", httpContentDistanza);
if (responseMessage.IsSuccessStatusCode)
string strContext = await responseMessage.Content.ReadAsStringAsync();
var risultato = JsonSerializer.Deserialize<Distanza1>(strContext);
catch(Exception ex)
var error = ex.Message;
它在 responseMessage = await httpClient.PostAsync("/xxx/xx/xxx", httpContentDistanza);
处崩溃,但在 ResetAll()
和 var x = this 处停止。
我以前看过并阅读过类似的问题(Debugger stops after async HttpClient.GetAsync() 和 HttpClient.PostAsync and await crashing app),但没有任何解决方案对我有帮助。 有什么关于如何捕捉异常的建议吗?
【问题讨论】:
也许检查您从 POST 获得的结果?如果它是一个未知的子站点,您将获得 404 HttpCode。也许这有帮助 怎么样?在ponseMessage = httpClient.PostAsync("/xxx/xx/xxx", httpContentDistanza).Result;
之后它崩溃并且没有到达任何代码行
您在同步上下文中调用了异步方法。所以基本上 PostAsync 不会阻塞并在另一个线程上下文中运行。
您需要做的是 a) 将 Async 调用放在异步方法中并用 await 装饰调用或 b) 为 HttpClient.Post 使用同步重载
Btw, the call and the respons must be instant, that's why I don't want to use Async and Await.
是什么让您认为 async / await 使其不那么即时?您不妨尝试一下 async / await - 它不会比您现在的速度慢。 ;)
【参考方案1】:
你应该改变这个:
public void TestX(string baseUrl)
到
public async Task TestX(string baseUrl)
还有这个
responseMessage = httpClient.PostAsync("/xxx/xx/xxx",httpContentDistanza).Result;
到
responseMessage = await httpClient.PostAsync("/xxx/xx/xxx", httpContentDistanza);
然后你就可以处理异常了。
【讨论】:
我以前做过,一开始。一样的。我只是更新我的问题以上是关于HttpClient.PostAsync 崩溃应用。如何捕捉异常?的主要内容,如果未能解决你的问题,请参考以下文章
HttpClient postasync,带有自定义标头和应用程序/json,用于正文 C#
网络条件差时如何处理文件HttpClient.PostAsync文件上传?
Xamarin Forms HttpClient PostAsync 总是抛出 NSMallocException