Skip to main content

Microsoft Silverlight

Answered Question Retrieve the HTTP Error code instead of "NotFound" with WebClientRSS Feed

(0)

thibhen
thibhen

Member

Member

1 points

6 Posts

Retrieve the HTTP Error code instead of "NotFound" with WebClient

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
                    }
                }
            );

How to retrieve correctly this error code ?

sasideva
sasideva

Member

Member

240 points

58 Posts

Re: Retrieve the HTTP Error code instead of "NotFound" with WebClient

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.";
}

Regards,
Deva

MarkMonster
MarkMonster

Contributor

Contributor

5220 points

1,046 Posts

Re: Retrieve the HTTP Error code instead of "NotFound" with WebClient

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.

Mark Monster - MCPD Enterprise
http://mark.mymonster.nl
Silverlight and Expression Insiders UG

Dont forget to click "Mark as Answer" on the post that helped you.

thibhen
thibhen

Member

Member

1 points

6 Posts

Re: Retrieve the HTTP Error code instead of "NotFound" with WebClient

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}µ

The error returned is "NotFound" (404?) instead of 500 with a custom error message.

I have the same problem with HttpWebRequest/HttpWebResponse. The same exception is raised on the code :
                        HttpWebResponse resp=
                            (HttpWebResponse)req.EndGetResponse(result);
The exception is raised before I am able to read the Http StatusCode
Any idea ?
Thanks for your help

thibhen
thibhen

Member

Member

1 points

6 Posts

Re: Retrieve the HTTP Error code instead of "NotFound" with WebClient

When I cast the Exception in WebException : the status is set to "UnknownError" (It is not was has been returned by the web server...).

MarkMonster
MarkMonster

Contributor

Contributor

5220 points

1,046 Posts

Re: Re: Retrieve the HTTP Error code instead of "NotFound" with WebClient

Strange I would expect a code like 404 when doing this:
((HttpWebReponse)((WebException)e.Error).Response).StatusCode

Mark Monster - MCPD Enterprise
http://mark.mymonster.nl
Silverlight and Expression Insiders UG

Dont forget to click "Mark as Answer" on the post that helped you.

Maud
Maud

Contributor

Contributor

3358 points

461 Posts

Answered Question

Re: Re: Retrieve the HTTP Error code instead of "NotFound" with WebClient

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.

Maud

thibhen
thibhen

Member

Member

1 points

6 Posts

Re: Re: Retrieve the HTTP Error code instead of "NotFound" with WebClient

Ok Thanks, I suppose I will have to write my own WebClient using Sockets (seems to be the pre history of computer science!).

saavedrah
saavedrah

Member

Member

11 points

13 Posts

Re: Re: Retrieve the HTTP Error code instead of "NotFound" with WebClient

 Hello Maud,

 Do you know if this has changed in Silverlight 3 with

HttpWebRequest.RegisterPrefix( "http://", System.Net.Browser.WebRequestCreator.ClientHttp );

Thanks.

 

 

 

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities