HttpWebRequest Exceptions under .NETCF

While testing code using HttpWebRequest it can be observed in the debug output that a number of exceptions are thrown within the GetResponse call of a HttpWebRequest. If you run exactly the same code on the desktop you don’t see this behaviour. For reference the following is a simple example which displays the issue:-

System.Net.WebRequest request = System.Net.WebRequest.Create(”);
System.Net.WebResponse webResponse = request.GetResponse();

Since the exceptions are caught it doesn’t stop the code from running but I considered it annoying enough to investigate and try to find the cause. Here is the typical output during the call to GetResponse:-

A first chance exception of type ‘System.IO.IOException’ occurred in mscorlib.dll
A first chance exception of type ‘System.UriFormatException’ occurred in System.dll
The thread 0x577c6eaa has exited with code 0 (0x0).
The thread 0xaf16af8a has exited with code 0 (0x0).
A first chance exception of type ‘System.UriFormatException’ occurred in System.dll
The thread 0x577c6eaa has exited with code 0 (0x0).
The thread 0xaf16af8a has exited with code 0 (0x0).
The thread 0xaf399a02 has exited with code 0 (0x0).

I eventually tracked it down to an issue with WebProxy. It occurs if you do not specify a Proxy or use the system proxy:-

request.Proxy = System.Net.GlobalProxySelection.Select;

If you won’t be using a proxy you can set the Proxy property to an empty WebProxy:-

request.Proxy = System.Net.GlobalProxySelection.GetEmptyWebProxy();

After making this change you’ll see the method progress without any exceptions – you’ll just see the 5 thread exit notifications in the output. Whether or not this makes a noticeable difference to performance I have yet to discover but it does indicate an underlying issue since the desktop has no such problem.

Published by Peter Freeman Foot

Microsoft Windows Development MVP

5 replies on “HttpWebRequest Exceptions under .NETCF”

  1. Ok, this is great! I’ve always found these first chance exceptions annoying, but having read something on the MSDN forums about not to worry about them because they are being caught by the framework, I stopped thinking about them.

    My output window is already much cleaner now.

    Now I only need to get rid of the first chance FileNotFoundException, UnauthorizedAccessException and IOException when loading images and then my output window is completely clean 🙂

  2. Great. I wrote you a mail to ask the reason for this exceptions.

    Now I’m building my own test application with Windows Mobile 6.0 Professional Emulator(PPC).

  3. Hi,

    When I try to use a WebBrowser component in a dialogue (WinCE-5.0) and try to open Web-Sites with lots of frames (For Example: The dialogue and hence the entire application crashes. I am also unable to catch the exception at upper layer, it simply crashes and I don’t get any exception in my Catch Block at application layer.

    When I try to open the same WebSite in WinCE-5.0 default internet explorer, the Web-Site opens up without problem.

    I thought it has something to do with .NETCF and have even tried it out with Native Code (WIN32 and MFC) and have used IWebBrowser2 but the same problem manifests. It simply crashes. I am unable to find a solution to this problem.

Comments are closed.