Freigeben über


How to debug HttpWebRequest errors in Windows Phone 8

If you use HttpWebRequest, WebRequest or related classes in your Windows Phone 8 application exceptions do not throw HTTP STATUS codes like the desktop or Windows Store apps do.  In fact, the exception always returns:

{System.Net.WebException: The remote server returned an error: NotFound. ---> System.Net.WebException: The remote server returned an error: NotFound.

This is not very useful when debugging because this is the same error for various HTTP error responses (whether they be 400 or 500 series).

For example when the server returns a 400 or 500 series error the exception is always the same (see above)!

The easiest way to see what is happening in development is simply use the emulator and inspect the traffic using Fiddler following this post Configure the Windows Phone 8 Emulator to work with Fiddler .

By using Fiddler you can immediately see the HTTP STATUS code and what is being transmitted and received in clear text.  In this case you can see a 502 (DNS failure) some 200 success and a 401 Authorization issue:

image

 

Another option is to extract the Status Code and act on that information in some way in your code with code similar to this:

Copy Code:

         private static void RespCallback(IAsyncResult asynchronousResult)
        {
            try
            {
                // State of request is asynchronous.
                RequestState myRequestState = (RequestState)asynchronousResult.AsyncState;
                HttpWebRequest myHttpWebRequest2 = myRequestState.request;
                myRequestState.response = (HttpWebResponse)myHttpWebRequest2.EndGetResponse(asynchronousResult);

                // Read the response into a Stream object.
                Stream responseStream = myRequestState.response.GetResponseStream();
                myRequestState.streamResponse = responseStream;

                // Begin the Reading of the contents of the HTML page and print it to the console.
                IAsyncResult asynchronousInputRead = responseStream.BeginRead(myRequestState.BufferRead, 
0, BUFFER_SIZE, new AsyncCallback(ReadCallBack), myRequestState);
            }
            catch (WebException e)
            {
                // get the HTTP_STATUS
                int statusCode = -1;

                if (e.Response != null)
                {
                    //try to cast to HttpWebResponse (it should be!)
                    HttpWebResponse aResp = e.Response as HttpWebResponse;

                    if (aResp !=null )
                    {
                        statusCode = (int)aResp.StatusCode;
                    }
                    else
                    {
                        // not an HttpWebResponse!
                    }
                }
                // Need to handle the exception some other way
            }
        }
        

 

This will allow you to extract the http status code itself and take appropriate action.  In general, once you have the HTTP STATUS code and the actually HTTP or HTTPS traffic from Fiddler you can easily start troubleshooting further!

For more on HTTP troubleshooting check out this blog Troubleshooting code that uses the HTTP protocol.

Let me know if this helps you out!

Follow the Windows Store Developer Solutions team on Twitter @wsdevsol.

Comments

  • Anonymous
    April 10, 2014
    Dose not work, "Unsupported Media Type"

  • Anonymous
    November 11, 2014
    Thanks very much, you help me a lot.