Friday, May 23, 2014

Mono / Unity3D C# HTTPS HttpWebRequest Asynchronous craziness

Beware, ye crabby developers, of using HttpWebRequest asynchronously with SSL connection because there appears to be some crazy bug that causes a timeout (defaults to 100 seconds if I recall correctly) that yields a null response.

I spent the entire morning (and I ran into this previously on iOS a year ago and put it aside for a day when I felt like bashing my head against rocks) wondering why some simple asynchronous download management code I'd written work wonderfully for FTP and HTTP but then nothing seemed to happen when using a URL with HTTPS as the protocol...

I did the usual things like ASSumed it was a self-signed cert issue so I overwrote the certificate acceptance code - no luck.  I spent a bunch of time trying to understand the error messages I was catching on exception, ultimately tracing it down to a timeout on the OTHER end of the connection yielding a "connection forcibly closed" which told me that my server was actually saying "look meathead, if you're just going to sit on the line I'm going to cut you off so I can help someone else..."

So, I started Googling.  An hour later after reading about a ton of other people having this issue (but nothing mentioning 'debug') I caught a little bugzilla entry mentioning 'odd behavior' with HttpWebRequest when running in DEBUG.  So, having exhausted all rational attempts to resolve the issue I said "screw it, let's see if it works in release, guffaw, chortle..."

Bang, 8 simultaneous HTTPS downloads worked like a charm.


So, if you too have crazy HTTPS issues that defy reason and you're using Mono/Xamarin/Unity3D, give your friendly neighborhood release mode build a try and see if it magically solves your ills.  I know mine did.

Hey Mav, do you still have the number of that truck driving school?  Truck Masters I think it was called...