Skip to main content
Home Forums Silverlight Programming Accessing Web Services with Silverlight Retrieve the HTTP Error code instead of "NotFound" with WebClient
8 replies. Latest Post by saavedrah on November 27, 2009.
(0)
thibhen
Member
1 points
6 Posts
10-30-2009 5:07 AM |
Hello, I am trying to catch the error returned by an external Web server but Silverlight always returned a generic error message : "The remote server returned an error: NotFound.". I would expect to have something like "Error 500 : Internal server Error".
WebClient client = new WebClient(); client.DownloadStringAsync( "http://myurl" ); client.DownloadStringCompleted += new DownloadStringCompletedEventHandler( delegate(object sender, DownloadStringCompletedEventArgs e) { if (e.Error != null) { // The bad error message } else { // process content } } );
sasideva
240 points
58 Posts
10-30-2009 5:37 AM |
First it should be completed event Then async call. some thing like this
WebClient client = new WebClient(); client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted); client.DownloadStringAsync(new Uri("/Silverlight.js", UriKind.Relative));
void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e) { status.Text = "Downloading...Done."; }
MarkMonster
Contributor
5220 points
1,046 Posts
10-30-2009 5:39 AM |
Did you check what the type of the Exception is?
If it's a WebException (You can cast the e.Error to WebException).
WebException.Response is the WebResponse (cast it to HttpWebResponse)
HttpWebResponse.StatusCode gives you the status code.
10-30-2009 6:51 AM |
Sasideva, The code given in example is async (see the delegate method for the callback). MarkMonster, The exception is
- e.Error {System.Net.WebException: The remote server returned an error: NotFound. ---> System.Net.WebException: The remote server returned an error: NotFound. at System.Net.Browser.BrowserHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult) at System.Net.Browser.BrowserHttpWebRequest.<>c__DisplayClass5.b__4(Object sendState) at System.Net.Browser.AsyncHelper.<>c__DisplayClass2.b__0(Object sendState) --- End of inner exception stack trace --- at System.Net.Browser.AsyncHelper.BeginOnUI(SendOrPostCallback beginMethod, Object state) at System.Net.Browser.BrowserHttpWebRequest.EndGetResponse(IAsyncResult asyncResult) at System.Net.WebClient.GetWebResponse(WebRequest request, IAsyncResult result) at System.Net.WebClient.DownloadBitsResponseCallback(IAsyncResult result)} System.Exception {System.Net.WebException}µ
HttpWebResponse resp= (HttpWebResponse)req.EndGetResponse(result);
10-30-2009 6:56 AM |
When I cast the Exception in WebException : the status is set to "UnknownError" (It is not was has been returned by the web server...).
10-30-2009 3:33 PM |
Strange I would expect a code like 404 when doing this: ((HttpWebReponse)((WebException)e.Error).Response).StatusCode
Maud
3358 points
461 Posts
11-03-2009 3:58 AM |
Hi thibhen,
I'm afraid this is by design that return general "not found" error for any kinds of network errors. I've test on my machine with fiddler, the server did return 500 error, however, webclient return 404.
Just like wcf in silverlight, service exception will be simply return "Not Found" exception on client by default.
11-03-2009 4:57 AM |
Ok Thanks, I suppose I will have to write my own WebClient using Sockets (seems to be the pre history of computer science!).
saavedrah
11 points
13 Posts
11-27-2009 12:58 AM |
Hello Maud,
Do you know if this has changed in Silverlight 3 with
HttpWebRequest.RegisterPrefix( "http://", System.Net.Browser.WebRequestCreator.ClientHttp );
Thanks.