C#と.NET 4.5を使用して404エラーが発生した場合に、のメソッドresponse
によって返されるものを特定しようとしています。HttpClient
GetAsync
現在、404やタイムアウトなどのエラーのステータスではなく、エラーが発生したことしかわかりません。
現在、私のコードは次のようになっています。
static void Main(string[] args)
{
dotest("http://error.123");
Console.ReadLine();
}
static async void dotest(string url)
{
HttpClient client = new HttpClient();
HttpResponseMessage response = new HttpResponseMessage();
try
{
response = await client.GetAsync(url);
if (response.IsSuccessStatusCode)
{
Console.WriteLine(response.StatusCode.ToString());
}
else
{
// problems handling here
string msg = response.IsSuccessStatusCode.ToString();
throw new Exception(msg);
}
}
catch (Exception e)
{
// .. and understanding the error here
Console.WriteLine( e.ToString() );
}
}
私の問題は、例外を処理できず、そのステータスやその他の問題の詳細を特定できないことです。
例外を適切に処理し、発生したエラーを解釈するにはどうすればよいですか?