Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 HajiyevEl · Apr 30, 2020 at 03:39 PM · unity 5webhttpwebrequestrequest

Can't fetch site as string. Unitywebrequest

How it will look in new UnityWebrequest?

 string result = String.Empty;
 while (connectionRetries < maxRetries)
                     {
                         WWW www = new WWW(mySite.url);
     
                         while (!www.isDone)
                             yield return null;
     
                         if (!string.IsNullOrEmpty(www.error))
                         {
                             connectionRetries++;
                             Debug.LogWarning("Error Loading My Site " + mySite.url + ". Retrying connection " + connectionRetries);
                             errorMessage = www.error;
                         }
                         else
                         {
                            result = www.text;
                             break;
                         }
                     }



It does work like this, but when i try changing to UnityWebRequest it doesnt. Can't fetch site as string.

 string result = String.Empty;
 while (connectionRetries < maxRetries)
                     {
                         UnityWebRequest www = UnityWebRequest.Get("mySite.url");
     
                         while (!www.isDone)
                             yield return null;
     
                         if (!string.IsNullOrEmpty(www.error))
                         {
                             connectionRetries++;
                             Debug.LogWarning("Error Loading My Site " + mySite.url + ". Retrying connection " + connectionRetries);
                             errorMessage = www.error;
                         }
                         else
                         {
                            result = www.downloadHandler.text;
                             break;
                         }
                     }

Also tried: result = www.downloadHandler.text.ToString();

Help?

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
2
Best Answer

Answer by Bunny83 · Apr 30, 2020 at 03:55 PM

You haven't actually send your webrequest. Unity's old WWW class immediately sends out the request when you create it. The UnityWebRequest allows more advanced requests where you can change the request headers and upload data after you created the web request. You have to call UnityWebRequest.SendWebRequest to actually send the request to the target server.


Note that busy waiting on isDone is not recommended and might break on certain platforms (like WebGL) since WebGL doesn't support multi threading (yet). It's always recommended to use a coroutine and yield on the result that the SendWebRequest method returns. Just have a look at the example code in the documenation

Comment
Add comment · Show 11 · 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 HajiyevEl · May 01, 2020 at 11:10 AM 0
Share

Still couldn't solve the problem, but thanks for answer anyway, at least got some useful information.

avatar image Bunny83 HajiyevEl · May 01, 2020 at 11:55 AM 1
Share

But what's your exact issue then? I just tried something very similar to what you have:

 string domain = "my.secret.domain";
 void Start()
 {
     StartCoroutine(LoadPage("https://"+ domain + "/UnityAnswers/ExampleContant.txt", s =>
     {
         Debug.Log("page content: " + s);
     }));
 }


 const int maxRetries = 3;
 IEnumerator LoadPage(string aURL, System.Action<string> aCallback)
 {
     int connectionRetries = 0;
     while (connectionRetries < maxRetries)
     {
         UnityWebRequest request = UnityWebRequest.Get(aURL);
         request.SendWebRequest();
         while (!request.isDone)
             yield return null;

         if (!string.IsNullOrEmpty(request.error))
         {
             connectionRetries++;
             Debug.LogWarning("Error Loading $$anonymous$$y Site " + aURL + ". Retrying connection " + connectionRetries);
         }
         else
         {
             if (aCallback != null) aCallback(request.downloadHandler.text);
             break;
         }
     }
 }

I've removed my domain name but it's essentially my dyn dns domain which points to my public IP and the request is handled by my raspberry pi which runs an apache server. I quickly created this example file on my server and Unity happily downloads the file. Are you sure that you can actually reach the endpoint (i.e. have you tried your URL in a browser?).


Some important things you might want to check:

  • Some routers don't allow a local loop back. So trying to connect to your own public IP from inside your NAT might not work. Of course this is not an issue if the server you want to reach is hosted elsewhere on the net.

  • If you use HTTPS be aware that selfsigned certificates are usually rejected by most clients since they are considered unsafe. I use a free Let's Encrypt certificate on my server.

  • If you use a freehoster somewhere on the internet, be aware that some of them implement anti bot / crawler protection by injecting an intermediate page with javascript which does essentially performs a redirect. Of course this won't work when you just read a certain URL with a webrequest. If your hoster has such a feature, try to disable it. If you can't disable it you really should change your hoster -.-

  • Note that some platforms have higher security standards than others. For example mobile build usually reject any unencrypted http requests nowadays. Depending on the target OS there might be workarounds. Though from what I've heard iOS is quite strict in this regards. Likewise when creating a WebGL build you are bound to the browsers "same origin" policy. So you can not simply load anything from a different domain than the one where your WebGL content is hosted. If you need to do this, lookup how to implement CORS on your server.

avatar image Bunny83 HajiyevEl · May 01, 2020 at 11:57 AM 0
Share

Just for a test, try loading something from pastebin. I've put this sample file on my pastebin. The raw URL of the content is:

 https://pastebin.com/raw/eSxsmsRd


avatar image opponent019 Bunny83 · Apr 06, 2021 at 12:59 AM 0
Share

I was having the same issue and I was particularly trying to read from PasteBin but kept getting "Unknown Error". I tried your link and it worked. My guess is that if they're too long it won't work (my pastebin has a ton of text). Just wanted to add this here in case someone else runs into the same issue.

Show more comments

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

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

UnityWebRequest - 403 Forbidden 0 Answers

Problem deserialize Json api rest 1 Answer

Does overriding UnityWWWRequestDefaultProvider works for UnityWebRequest? 0 Answers

How to stop mono from preventing authentication 2 Answers

UnityWebRequest returning responseCode 0 and isNetworkError == true for Unity 2019 1 Answer


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