- Home /
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:
check for internet connectivity.
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...
Answer by Briksins · Dec 17, 2013 at 12:21 PM
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:
<?php
if(isset($_POST['fileNameToDownload']))
{
$file = 'C:/MyServer/MyFiles/'.$_POST['fileNameToDownload'];
//$file = 'https://dl.dropboxusercontent.com/u/15715229/tempImg.jpg';
//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));
ob_clean();
flush();
readfile($file);
exit;
}
}
else
{
echo "damn you! you didnt provide file name trough POST";
}
?>
All this seems quite amazing! I'll take some time to absorb, try and test everything. Just wanted to give you a heads up and a big thank you so far! I'm not very fond of the php
script, but maybe that is indeed the best / only good way.
Hey :) no problem at all, im happy to help! Unity has great community! If your application contains some online features (connecting to some type of server through Web requests or sockets) it is good practice to have a server at place which double check and control what ever client is allowed to do. Even if your server just supplying client with a files it would be good practice to implement additional check on the server side for whom do you supply this files and if client who request a file is actually your client for whom this file should be delivered. other way you can supply files to hackers internet browser :)
good luck and keep posting if you have more problems
Also I give example in PHP just because its very easy, however you can have server written on anything you like Java/C#/Phyton/C/C++ etc... It is up to you how and in which environment to handle server requests
Your answer
Follow this Question
Related Questions
POST to HTTP not working 2 Answers
WWW Class behaves strangely on iOS 3 Answers
SSLHandshakeException during Web Request 0 Answers
Download multiple files from server 2 Answers
What is the equivalent of unityWebRequest.downloadHandler.text when using WWW? 0 Answers