Silverlight: HttpWebResponse.StatusCode other than 200 OK or 404 Not Found

by bazzilic

I was writing a Silverlight application and needed to split behavior depending on the status code returned by GET HTTP-request to a server. “This should be easy”, — I thought and wrote the following code:

    var request = WebRequest.Create(uri) as HttpWebRequest;
    request.BeginGetResponse(new AsyncCallback(OnGetRequestFinished), request);

 // ...

private void OnGetRequestFinished(IAsyncResult GetAsyncResult)
{
    var request = GetAsyncResult.AsyncState as HttpWebRequest;
    var response = get_request.EndGetResponse(GetAsyncResult) as HttpWebResponse;
    if (response.StatusCode == HttpStatusCode.Accepted)
        DoSomething1();
    else if (response.StatusCode == HttpStatusCode.OK)
        DoSomething2();
    else
        ProcessError();
}

However, while server was seemingly sending status code 202 Accepted, my Silverlight client was falling into executing DoSomething2();. Luckily, I was writing both server and client side, so I digged into the server code to check if I was really sending status code 202. As a matter of fact — I was, which meant that server was sending code 202 and client was receiving code 200. “What if it is modified somewhere in the middle?”, — I thought and began examining network traffic with tcpdump.

First, I checked outgoing traffic on the server side. The status code was 202. Then I looked into the received traffic on the client side (with a little help from MicroOLAP TCPDUMP for Windows). Status code was still 202, but client continued acting as if it received status code 200. “What the fuck?!”, — I thought and began googling, which was giving no results for a wide range of queries until I accidentally found this document on MSDN.

To make a long story short, it appeared that by default Silverlight utilizes browser networking system which, for security or whatever other reasons, yields status code 404 Not Found in case of any error and status code 200 Accepted otherwise. I hope they really have a good reason for this.

Good news were that it is possible to use Silverlight built-in HTTP client instead of utilizing browser functionality, as described here. So I rewrote the code and it worked perfectly:

    var request = WebRequestCreator.ClientHttp.Create(uri) as HttpWebRequest;
    request.BeginGetResponse(new AsyncCallback(OnGetRequestFinished), request);

 // ...

private void OnGetRequestFinished(IAsyncResult GetAsyncResult)
{
    var request = GetAsyncResult.AsyncState as HttpWebRequest;
    var response = get_request.EndGetResponse(GetAsyncResult) as HttpWebResponse;
    if (response.StatusCode == HttpStatusCode.Accepted)
        DoSomething1();
    else if (response.StatusCode == HttpStatusCode.OK)
        DoSomething2();
    else
        ProcessError();
}

Yes, that simple, just changed the only one line of code — hardly comparable to the time I spent looking for the solution! Details on the WebRequestCreator could be found in MSDN here.

Hopefully, this post would eventually save someone’s time!