前言
开发过程中使用flurl.http发送http请求,但是服务器有时候会返回400、404这样的错误,有时候会携带信息,比如json提示或者xml或者html的提示,一开始不知道怎么捕捉,需要结合apipost调试,经过百度查找,可以在错误捕获服务器返回的信息
代码
try
{
//http请求
}
catch (FlurlHttpException ex)
{//获取原始字符串,支持类型多var errDetail = await ex.GetResponseStringAsync();//也可以获取json,确定返回json直接转化为对象更合适var statusCode = await ex.GetResponseJsonAsync<myResponse>();throw new Exception(errDetail);}
参考https://stackoverflow.com/questions/55020852/how-to-handle-bad-request-exception-in-flurl