UnityWebRequestTexture.GetTexture() web request is silently failing

I’m trying to scrape images from websites but some of the URL’s that I try to download images from don’t work. So for example, if I try to use UnityWebRequestTexture.GetTexture() with this URL (https://cdn.shopify.com/s/files/1/1963/1339/files/G20-full-right-front.jpg) I’m able to download the image but if I use this URL
(https://pisces.bbystatic.com/image2/BestBuy_US/images/products/6427/6427116_sd.jpg) I get nothing

I’ve stepped through the code in a debugger and it looks like it’s silently failing on the SendWebRequest(), it never gets past this point.

Here is my code:

private IEnumerator LoadImage(string url)
   {
      using (var www = UnityWebRequestTexture.GetTexture(url))
      {
         yield return www.SendWebRequest();
         if (www.isNetworkError)
         {
            Debug.Log(www.error);
         }
         else
         {
         
            Texture2D texture = null;
            try
            {
               texture = DownloadHandlerTexture.GetContent(www);

            }
            catch (Exception)
            {
               Destroy(gameObject);
               yield break;
            }

            // do something with the texture
            ...
         }
      }
   }

Probably a CORS issue I’m guessing, or else some kind of SSL cert issue. Check the logs for your target.

Debugging networking, UnityWebRequest, WWW, Postman, curl, WebAPI, etc:

And setting up a proxy can be very helpful too, in order to compare traffic:

This is an incomplete check, the request might fail due to HTTP error. And that’s what probably happens, since when I try to access the second link in browser, I get “Access Denied”.

1 Like

I used Charles Proxy, which shows that the connection was established with a 200 response code. So the request is getting sent but there is never a response from the server. So when I call www.SendWebRequest(); it never returns so I never get a HTTP error. (I’ve added more network checks like Aurimas-Cernius suggested, but these checks never get called).

I’m thinking that it’s a CORS issue, my guess is that the server has some CORS rule that does not permit me to load its resources. I recently tried adding “Access-Control-Allow-Origin : *” to my header to see if I can make a request without credentials, but this produced no response from the server.

Assuming that there is a CORS issue what else can I do? Also, how can I know what CORS-related rules the server is enforcing?

Don’t forget to call LoadImage function with StartCoroutine. Call,

StartCoroutine(LoadImage(url));