Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by cregox · Jun 06, 2013 at 08:35 PM · wwwwebfileswebrequestasync

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...

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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";
   }
 
 ?>

  


Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image cregox · Dec 18, 2013 at 03:13 PM 1
Share

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.

avatar image Briksins · Dec 18, 2013 at 03:21 PM 0
Share

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

avatar image Briksins · Dec 18, 2013 at 03:29 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges