从多个网址

时间:2017-09-22 09:57:55

标签: c# url unity3d download

我需要从URL_1下载该文件。如果该文件不可用(返回404),则在URL_1上,脚本必须尝试从URL_2下载该文件。

现在我正在使用HttpWebRequest,但检查文件的可用性需要很长时间。

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

提前致谢:)

2 个答案:

答案 0 :(得分:1)

  

现在我正在使用HttpWebRequest,但是检查它需要很长时间   我文件的可用性

那是因为你没有在另一个Thread中这样做,所以它会阻止主线程直到请求完成。使用HttpWebRequest需要线程和way在完成后通知主线程。

您应该使用Unity的WWWUnityWebRequest在协程中发出该请求。这两个API将在后台提出Thread的问题。你只需等待它就可以完成一个协程。您可以在Action的帮助下组织多个请求。

一个简单的可重用下载功能,成功时返回true:

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

按顺序发出请求(3个请求):

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

用法:

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());
}

答案 1 :(得分:0)

您可以使用WWW类:

执行此操作
<?php

    $product_name  = "99X210MM DL - SINGLE SUPPLY & ATTACHMENT"; // not working with this
    $product_name1 = "99X210MM DL SINGLE SUPPLY TTACHMENT"; // working with this

    $auth   = "------------------------"; //can't share 
    $url    ="https://crm.zoho.com/crm/private/json/Products/searchRecords";
    $query  ="authtoken=".$auth."&scope=crmapi&criteria=(Product Name:".$product_name.")"; 
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 30);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $query);
    $response = curl_exec($ch);
    curl_close($ch);
    $rslt = json_decode($response); 
    echo "<pre>"; print_r($rslt);

?>

如果错误为空,则找到资源,否则错误尝试另一个网址。