- Home /
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?
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
Still couldn't solve the problem, but thanks for answer anyway, at least got some useful information.
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.
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
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.
Your answer
Follow this Question
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