简体   繁体   中英

Download file from multiple URLs

I need to download the file from URL_1. If the file is not available (returns 404), on URL_1, script has to try to download the file from URL_2.

Now I'm using HttpWebRequest , but it takes too long to check the availability of my file.

private bool IsPageExists(string url)
{
    Uri uri = new Uri(url);
    try
    {
        HttpWebRequest httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(uri);
        HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
    }
    catch
    {
        return false;
    }
    return true;
}

Thanks in advance :)

Now I'm using HttpWebRequest, but it takes too long to check the availability of my file

That's because you are not doing that in another Thread so it will like block the main Thread until the request is done. Using HttpWebRequest requires threading and a way to notify the main thread when you are done.

You should use Unity's WWW or UnityWebRequest to make that request in a coroutine. These two API will make the question with Thread in the background. You just wait for it to finish with a coroutine. You can organize multiple requests with the help of Action .

A simple re-usable download function that returns true on success:

IEnumerator getRequest(string uri, Action<bool> success)
{
    UnityWebRequest uwr = UnityWebRequest.Get(uri);
    yield return uwr.SendWebRequest();

    //Check if there is an error
    if (uwr.isError || !String.IsNullOrEmpty(uwr.error))
    {
        success(false);
    }
    else
    {
        success(true);
        //Store downloaded data to the global variable
        downloadedData = uwr.downloadHandler.data;
    }
}

To make requests in order(3 requests):

IEnumerator downloadMultipleFile()
{
    bool success = false;

    //Download first file
    yield return getRequest(url1, (status) => { success = status; });

    //Check if download was successful
    if (success)
    {
        Debug.Log("File downloaded from the first url");
        //Use downloadedData here

        //Exit
        yield break;
    }
    Debug.Log("Error downloading from first url");

    //Download second file
    yield return getRequest(url2, (status) => { success = status; });

    //Check if download was successful
    if (success)
    {
        Debug.Log("File downloaded from the second url");
        //Use downloadedData here

        //Exit
        yield break;
    }
    Debug.Log("Error downloading from second url");

    //Download third file
    yield return getRequest(url3, (status) => { success = status; });

    //Check if download was successful
    if (success)
    {
        Debug.Log("File downloaded from the second url");
        //Use downloadedData here

        //Exit
        yield break;
    }
    Debug.Log("Error downloading from third url");
}

Usage:

string url1 = "https://images.pexels.com/photos/38265/flower-blossom-bloom-winter-38265.jpeg";
string url2 = "https://images.pexels.com/photos/404313/pexels-photo-404313.jpeg";
string url3 = "https://images.pexels.com/photos/65891/prairie-dog-rodent-animals-cynomys-65891.jpeg";

byte[] downloadedData;

void Start()
{
    StartCoroutine(downloadMultipleFile());
}

You can do this using WWW class:

IEnumerator WWWClass()
    {
        string url1 = "xyz.com";
        string url2 = "xyzzz.com";
        WWW www = new WWW(url);
        yield return www;
        if (www.error == null)
        {
            //sucess
        }
        else {
            //unscuess
            www = new WWW(url2);
            yield return www;
            if (www.error == null)
            {
                //sucess
            }
            else {
                //unscuess
            }
        }
    }

if error is null then resource found else it is error try another url.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM