Using async WebRequest GetResponse

Disclaimer: This is a friggin’ really complicated question. Probably what I wasted the most time out of anything I’ve ever done with Unity 3D in the past 5 years or so. And I’m still stuck here… So, be aware about all links before trying to answer! (although this will be most likely unanswered for evah!)

I need 2 things:

  1. check for internet connectivity.
  2. compare file bundle versions to see if differs from local and web.

WebRequest could solve both. I could get headers on any URL, thus verifying not only internet connectivity, but also if the server is up, without relying on unreliable ping. Also I could get the file length, without needing to download it all first with WWW, and that would be good enough to check if the file is different.

First problem is that method GetResponse isn’t asynchronous like WWW and so it locks up the whole application while it is checking. When doing for several, hundreds, of files, it sums up a lot. Second problem is that BeginGetResponse is so complicated to implement that I found it impossible until now.

I’ve built many versions of a fine script, I called it WebAsync, but now I just can’t get around the issues on why WebResponse is failing. I do know it can be way more complex than I would expect, but even then, it still gives me random results when I test, still bringing null when it shouldn’t.

Just today I decided to drop WebAsync in favor of WWW, and it was working much better already… But then I stumbled upon the already mentioned problem with getting file length and now I think it won’t work because of need #2. Unless I drop the idea of using the file length and use a separated bundle version control file, kinda like unity do with external version control, and create a “meta” for each bundle.

So, there are actually 2 kind of right answers (as usual) here. For the title, or for these needs (if there is another option). But, please, don’t tell me I’ll have to go forward with the last idea and keep meta files everywhere! I hate that idea…

Hi there!
There much more solutions than just 2 you mention and most of them are not what you mention

first of all you shouldn’t do web request in the main thread as webrequest are time consuming and will hang your GUI untill request is completed. You should use asynchronous calculation

how to do it? I just explain it here

now lets back to web request,
from what I see you have 2 best options:

  1. use native unity WWW with StarcCorutine and “yield”

  2. use delegates with BegineEnvoke and EndEnvoke where you will not be able to use WWW as it is Unity main thread depending, but you easily can use native C# class WebCleint.

WebClient also has headers and one of them “Content-Length” which can tell you the size of the file without downloading it.

and finally you can create some PHP script on server side to respond you with file length and let you to download file manually

something like that:


        $file = 'C:/MyServer/MyFiles/'.$_POST['fileNameToDownload'];
        //$file = '';
        //echo "File Base Name: ".basename($file)." File size: ".linkinfo($file);
        if (file_exists($file)) {
            header('Content-Description: File Transfer');
            header('Content-Type: application/octet-stream');
            header('Content-Disposition: attachment; filename='.basename($file));
            header('Content-Transfer-Encoding: binary');
            header('Expires: 0');
            header('Cache-Control: must-revalidate');
            header('Pragma: public');
            header('Content-Location: ' . filesize($file));
      echo "damn you! you didnt provide file name trough POST";