I've been scratching my head on this for a while. I have no clue where this exception is being thrown. Where the heck am I supposed to catch this exception? It's a System.Net.WebException
. I guess I'm just not getting Task and async/await. I understand I'm creating other threads, which is great and all, but where do I handle the exception?
The remote server returned an error: (403) Forbidden.
Here is the code:
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");
}
Here is what I see in my Output window:
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.
Edit:
I updated makeAsyncRequest(HttpWebRequest)
per Stephen Cleary suggestion. I'm still having the same problem.
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;
}
Output window:
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.