GetResource WebException

时间:2019-03-28 19:04:42

标签: c# webclient system.net.webexception

我有此方法TResult GetResource,我需要添加一个WebException,但它正在寻找返回DeserializeObject的方法。处理此方法中的错误的最佳方法是什么?

我是否只返回诸如“错误”之类的Deserialize TResult字符串?

protected TResult GetResource<TResult>(string url)
{
    using (var webClient = new WebClient())
    {
        try
        {
            var response = Encoding.UTF8.GetString(webClient.DownloadData(url));
            //Debug.WriteLine("JSON Response " + response);
            return JsonConvert.DeserializeObject<TResult>(response);
        }
        catch (WebException ex)
        {
            return JsonConvert.DeserializeObject<TResult>("Error");

        }
    }
}

0 个答案:

没有答案