mardi 1 septembre 2015

HttpWebRequest.GetResponse() throws untrappable error

I am testing a web-service app I wrote in C# (Visual Studio 2015) on a Windows 2003 server. The app runs fine, but when I run the line of code:

WebResponse webResponse = request.GetResponse();

where request is an HttpWebRequest instance, the application throws an "Object reference not set to an instance of an object."

Before everyone tells me to go research that error, I have. It doesn't make sense here. The HttpWebRequest is certainly not null, I can access it's properties like URL and UserAgent right before the program crashes. Even more odd, this code is wrapped in a try..catch that traps both web exceptions and general exceptions. I've no idea why it isn't trapping this.

But the reason it is happening is because if I try the URL in Internet Explorer (version 8.0 on this old server), I get an "Internet Explorer cannot display the web page" error. I can't even sign in to the site I wish to access. Firefox works fine, but I don't know how to force HttpWebRequest to "be" Firefox (UserAgent has been set to a standard Mozilla string...).

If the web request layer on this old server just won't work (.NET version is 3.5, by the way), that's fine. But I'd like to be able to trap the error. Why doesn't my try..catch work, and does anyone have any idea what would make this very simple code (that works everywhere else) throw a null object error?

Thanks in advance for any insight anyone can offer -- greatly appreciated!




Aucun commentaire:

Enregistrer un commentaire