私はしばらくこれについて頭を悩ませてきました。この例外がスローされている場所がわかりません。この例外をどこでキャッチする必要があるのでしょうか。それはSystem.Net.WebException
です。Task と async/await を取得していないだけだと思います。他のスレッドを作成していることは理解していますが、これは素晴らしいことですが、どこで例外を処理すればよいでしょうか?
The remote server returned an error: (403) Forbidden.
コードは次のとおりです。
public async void sendRequest(string database, string table, string where, bool isPost, int deviceType)
{
string urlQuery = Constants.URL_QUERY + database + "/" + table + "?" + where;
Debug.WriteLine("URL Query: " + urlQuery);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(urlQuery);
request.Method = isPost ? "POST" : "GET";
request.ContentType = "application/json";
try
{
Debug.WriteLine("getting response");
WebResponse response = await makeAsyncRequest(request);
Debug.WriteLine("response obtained");
finishRequest(response);
}
catch (WebException e)
{
Debug.WriteLine("WebException caught");
}
}
private async static Task<WebResponse> makeAsyncRequest(HttpWebRequest req)
{
Task<WebResponse> task = Task.Factory.FromAsync<WebResponse>(req.BeginGetResponse, req.EndGetResponse, null);
await task.ContinueWith(t =>
{
if (t.IsFaulted)
{
Debug.WriteLine("It's faulted.");
t.Exception.Handle((x) =>
{
if (x is WebException)
{
Debug.WriteLine("It's a web exception");
return true;
}
Debug.WriteLine("Exception?: " + x);
return false;
});
}
});
Debug.WriteLine("returning task");
return task.Result;
}
private static void finishRequest(WebResponse response)
{
Debug.WriteLine("finishRequest called");
}
出力ウィンドウに表示される内容は次のとおりです。
2014-10-07 14:05:05.104 FormsTemplateiOS[1568:53280] getting response
[0:] getting response
Thread started: #3
Thread started: <Thread Pool> #4
Thread started: <Thread Pool> #5
Thread started: <Thread Pool> #6
Thread started: <Thread Pool> #7
Thread started: #8
[0:]
2014-10-07 14:05:05.681 FormsTemplateiOS[1568:53326] It's faulted.
[0:] It's faulted.
[0:]
2014-10-07 14:05:05.804 FormsTemplateiOS[1568:53326] It's a web exception
[0:] It's a web exception
Unhandled Exception:
System.Net.WebException: The remote server returned an error: (403) Forbidden.
Unhandled Exception:
System.Net.WebException: The remote server returned an error: (403) Forbidden.
at System.Net.HttpWebRequest.CheckFinalStatus (System.Net.WebAsyncResult result) [0x0033b] in ///Library/Frameworks/Xamarin.iOS.framework/Versions/8.0.0.63/src/mono/mcs/class/System/System.Net/HttpWebRequest.cs:1718
at System.Net.HttpWebRequest.SetResponseData (System.Net.WebConnectionData data) [0x00165] in ///Library/Frameworks/Xamarin.iOS.framework/Versions/8.0.0.63/src/mono/mcs/class/System/System.Net/HttpWebRequest.cs:1485
The program 'Mono' has exited with code 0 (0x0).
Debugging session ended.
編集:
makeAsyncRequest(HttpWebRequest)
Stephen Cleary の提案に従って更新しました。私はまだ同じ問題を抱えています。
private async static Task<WebResponse> makeAsyncRequest(HttpWebRequest req)
{
Task<WebResponse> task = Task.Factory.FromAsync<WebResponse>(req.BeginGetResponse, req.EndGetResponse, null);
try
{
return await task;
}
catch (WebException e)
{
Debug.WriteLine("It's a WebException");
}
catch (Exception e)
{
Debug.WriteLine("Other Exception");
}
return null;
}
出力ウィンドウ:
2014-10-07 14:32:08.385 FormsTemplateiOS[1588:57008] getting response
[0:] getting response
Thread started: #3
Thread started: <Thread Pool> #4
Thread started: <Thread Pool> #5
Thread started: <Thread Pool> #6
Thread started: <Thread Pool> #7
Thread started: #8
Unhandled Exception:
System.Net.WebException: The remote server returned an error: (403) Forbidden.
Unhandled Exception:
System.Net.WebException: The remote server returned an error: (403) Forbidden.
at System.Net.HttpWebRequest.CheckFinalStatus (System.Net.WebAsyncResult result) [0x0033b] in ///Library/Frameworks/Xamarin.iOS.framework/Versions/8.0.0.63/src/mono/mcs/class/System/System.Net/HttpWebRequest.cs:1718
at System.Net.HttpWebRequest.SetResponseData (System.Net.WebConnectionData data) [0x00165] in ///Library/Frameworks/Xamarin.iOS.framework/Versions/8.0.0.63/src/mono/mcs/class/System/System.Net/HttpWebRequest.cs:1485
The program 'Mono' has exited with code 0 (0x0).
Debugging session ended.