A solution to WebException trouble with .NET Compact Framework

I spent almost half a day to find a solution to a problem I had developing a little Windows Mobile application at work. This app downloads a CAB file via HTTP to install it on the device. I am using HttpWebRequest and Response classes from the .NET Compact Framework to do the download and it worked fine as long as I downloaded the file from server in our company’s network. As soon as I tried to download the file from a server on the internet which requires to use our company’s web proxy I got WebExceptions (ServerProtocolViolation: “The response did not contain an end of entity mark.”) whenever the getResponse is called on the HttpWebRequest.

After some research I found out that reason for these exceptions seems like to be that the proxy is manipulating the HTTP Header in a way the .NET Compact Frameworks parser does not like. In regular – non Windows Mobile – .NET Framework applications you could use an app.config file to set the parser to a less strict configuration by setting UseUnsafeHeaderParsing to true:

<?xml version="1.0" ?>
<httpWebRequest useUnsafeHeaderParsing="true" />

Unfortunately there is no way to do this configuration for a Windows Mobile .NET Compact Framework app.
Finally this forum post lead me to the very simple solution of my problem.
Just setting the HTTP ProtocolVersion of the WebRequest to version 1.0 fixed my problem:

request.ProtocolVersion = System.Net.HttpVersion.Version10;

One simple line of code was the output of almost half a day of online searching and testing, at least the app now downloads the file no matter if the server is behind a proxy or directly accessed in our LAN.

Leave a Reply

Your email address will not be published. Required fields are marked *