Rick Strahl's Weblog  

Wind, waves, code and everything in between...
.NET • C# • Markdown • WPF • All Things Web
Contact   •   Articles   •   Products   •   Support   •   Advertise
Sponsored by:
West Wind WebSurge - Rest Client and Http Load Testing for Windows

WinInet Apps failing when Internet Explorer is set to Offline Mode


:P
On this page:

Ran into a nasty issue last week when all of a sudden many of my old applications that are using WinInet for HTTP access started failing. Specifically, the WinInet HttpSendRequest() call started failing with an error of 2, which when retrieving the error boils down to:

WinInet Error 2: The system cannot find the file specified

Now this error can pop up in many legitimate scenarios with WinInet such as when no Internet connection is available or the HTTP configuration (usually configured in Internet Explorer’s options) is misconfigured. The error typically means that the server in question cannot be found or more specifically an Internet connection can’t be established.

In this case the problem started suddenly and was causing some of my own applications (old Visual FoxPro apps using my own wwHttp library) and all Adobe Air applications (which apparently uses WinInet for its basic HTTP stack) along with a few more oddball applications to fail instantly when trying to connect via HTTP. Most other applications – all of my installed browsers, email clients, various social network updaters all worked just fine. It seems it was only WinInet apps that were failing. Yet oddly Internet Explorer appeared to be working.

So the problem seemed to be isolated to those ‘classic’ applications using WinInet. WinInet’s base configuration uses the Internet Explorer options dialog. To check this out I typically go to the Internet Explorer options and find the Connection tab, and check out the LAN Setup. Make sure there are no rogue proxy settings or configuration scripts that are invalid. Trying with Auto-configuration on and off also can often fix ‘real’ configuration errors. This time however this wasn’t a problem – nothing in the LAN configuration was set (all default). I also played with the Automatic detection of settings which also had no effect.

I also tried to use Fiddler to see if that would tell me something. Fiddler has a few additional WinInet configuration options in its configuration. Running Fiddler and hitting an HTTP request using WinInet would never actually hit Fiddler – the failure would occur before WinInet ever fired up the HTTP connection to go through the Fiddler HTTP proxy.

And the Culprit is: Internet Explorer’s Work Offline Option

The culprit in this situation was Internet Explorer which at some point, unknown to me switched into Offline Mode and was then shut down:

WorkOfflineMode[4]

When this Offline mode is checked when IE is running *or* if IE gets shut down with this flag set, all applications using WinInet by default assume that it’s running in offline mode. Depending on your caching HTTP headers and whether the page was cached previously you may or may not get a response or an error. For an independent non-browser application this will be highly unpredictable and likely result in failures getting online – especially if the application forces requests to always reload by disabling HTTP caching (as I do on most of my dynamic HTTP clients).

What makes this especially tricky is that even when IE is in offline mode in the browser, you can still browse around the Web *if* you have a connection. IE will try to load anything it has cached from the local cache, but as soon as you hit a URL that isn’t cached it will automatically try to access that URL and uncheck the Work Offline option. Conversely if you get knocked off the Internet and browse in IE 9, IE will automatically go into offline mode. I never explicitly set offline mode – it just automatically sets itself on and off depending on the connection. Problem is if you’re not using IE all the time (as I do – rarely and just for testing so usually a few commonly used URLs) and you left it in offline mode when you exit, offline mode stays set which results in the above head scratcher. Ack.

This isn’t new behavior in IE 9 BTW – this behavior has always been there, but I think what’s different is that IE now automatically switches between online and offline modes without notifying you at all, so it’s hard to tell when you are offline.

Fixing the Issue in your Code

If you have an application that is using WinInet, there’s a WinInet option called INTERNET_OPTION_IGNORE_OFFLINE. I just checked this out in my own applications and Internet Explorer 9 and it works, but apparently it’s been broken for some older releases (I can’t confirm how far back though) – lots of posts seem to suggest the flag doesn’t work. However, in IE 9 at least it does seem to work if you call InternetSetOption before you call HttpOpenRequest with the Http Session handle.

In FoxPro code I use:

DECLARE INTEGER InternetSetOption ;

   IN WININET.DLL ;

   INTEGER HINTERNET,;

   INTEGER dwFlags,;

   INTEGER @dwValue,;

   INTEGER cbSize

lnOptionValue = 1   && BOOL TRUE pass by reference

 

*** Set needed SSL flags

lnResult=InternetSetOption(this.hHttpSession,;

   INTERNET_OPTION_IGNORE_OFFLINE ,;  && 77

   @lnOptionValue ,4)

 

DECLARE INTEGER HttpOpenRequest ;

   IN WININET.DLL ;

   INTEGER hHTTPHandle,;

   STRING lpzReqMethod,;

   STRING lpzPage,;

   STRING lpzVersion,;

   STRING lpzReferer,;

   STRING lpzAcceptTypes,;

   INTEGER dwFlags,;

   INTEGER dwContextw

 

 

hHTTPResult=HttpOpenRequest(THIS.hHttpsession,;

   lcVerb,;

   tcPage,;

   NULL,NULL,NULL,;

   INTERNET_FLAG_RELOAD + ;

   IIF(THIS.lsecurelink,INTERNET_FLAG_SECURE,0) + ;

   this.nHTTPServiceFlags,0)

… 

And this fixes the issue at least for IE 9…

In my FoxPro wwHttp class I now call this by default to never get bitten by this again… This solves the problem permanently for my HTTP client. I never want to see offline operation in an HTTP client API – it’s just too unpredictable in handling errors and the last thing you want is getting unpredictably stale data. Problem solved but this behavior is – well ugly. But then that’s to be expected from an API that’s based on Internet Explorer, eh?

Posted in HTTP  Windows  

The Voices of Reason


 

Eric Lawrence [MSFT]
June 20, 2011

# re: WinInet Apps failing when Internet Explorer is set to Offline Mode

I'm not sure I really understand the complaint. If the user wants the system to be put into offline mode, why would you expect network requests to ignore that user's preference?

Rick Strahl
June 20, 2011

# re: WinInet Apps failing when Internet Explorer is set to Offline Mode

@Eric - there are two problems here: a) IE goes into offline mode automatically and system wide. b) IE and WinInet do not automatically detect that offline mode is no longer required.

The scenario I described: Start IE w/o connection going into offline mode, shut it down then run a WinInet app with a connection is totally unexpected behavior especially since IE sets offline mode silently and it affects your entire system.

If it worked consistently (ie. automatically detect whether it's needed) it'd be a heck of a lot more useful. As it stands though it's very inconsistent with IE controlling system wide settings that in many cases SHOULD NOT apply to other WinInet applications.

There aren't many scenarios in my applications that access HTTP that require offline mode - in most scenarios that's a completely worthless feature and it's not a user preference in those cases because IE set that flag. And even if the user explicitly set offline mode he probably doesn't understand or expect it to be system wide.

Further it seems (couldn't verify for myself) the option described in the post didn't work for previous versions at all so you didn't even have the option of bypassing offline mode at all... thankfully it works now.

Few scenarios: Doing Internet Connectivity checks (checking if a site is up on app startup - heck you do that in Fiddler!), grabbing real-time data from the Web, all that fails badly in offline mode. Both would result in either bad data, or a support call to the producer. HTTP client applications tend to have very different needs than a browser does since the API is usually hidden and not controlled by the user, but by developers. Offline mode CAN be useful in non-browser apps no doubt, but it's probably the much less common (10-90%) scenario.

Frank Dietrich
July 23, 2011

# re: WinInet Apps failing when Internet Explorer is set to Offline Mode

Rick,

I fell over this literally ages ago and I remember it gave me a bad headache back then.

In earlier times, when we used to have modems or other dial-up devices, this used to happen quite often

With former versions of IE a registry key could be queried (and set if I'm right) and I could at least show a message to the user or add a protocol entry.

As You said this happens completely silent and most users don't even know the option is there.

I think it's a remainder of the old times when online-time used to be expensive and and we had no ajax or dynamic content. I assume the idea was to download or quickly browse through pages and do detailed reading later from cache.


Kind regards from Berlin

Frank

West Wind  © Rick Strahl, West Wind Technologies, 2005 - 2024