- Home /
Unity web request sometimes appear Error:System.Exception: Code:0 Error:Cannot resolve destination host
My problem is that I use unity web request for hotfix. 90% of the time, players can perform web reqeust normally. Sometimes you will encounter Error:System.Exception: Code:0 Error:Cannot resolve destination host error. I'm pretty sure that the player's network is fine, because when an error occurs, he sends the event back to my server. I'm also sure that the server of the download source is also fine, because I have passed the cdn of azure and google, and it is unlikely that these two big companies have problems together. Would like to ask if there are any developers who have encountered the same problem, or know how to solve this problem, thank you very much!
public class UnityWebRequestDownloader : AbstractDownloader
{
private static readonly ILog log = LogManager.GetLogger(typeof(UnityWebRequestDownloader));
public UnityWebRequestDownloader(Uri baseUri) : this(baseUri, SystemInfo.processorCount * 2)
{
}
public UnityWebRequestDownloader(Uri baseUri, int maxTaskCount) : base(baseUri, maxTaskCount)
{
}
protected override IEnumerator DoDownloadBundles(IProgressPromise<Progress, bool> promise, List<BundleInfo> bundles)
{
long totalSize = 0;
long downloadedSize = 0;
Progress progress = new Progress();
List<BundleInfo> list = new List<BundleInfo>();
for (int i = 0; i < bundles.Count; i++)
{
var info = bundles[i];
totalSize += info.FileSize;
if (BundleUtil.Exists(info))
{
downloadedSize += info.FileSize;
continue;
}
list.Add(info);
}
progress.TotalCount = bundles.Count;
progress.CompletedCount = bundles.Count - list.Count;
progress.TotalSize = totalSize;
progress.CompletedSize = downloadedSize;
yield return null;
List<KeyValuePair<BundleInfo, UnityWebRequest>> tasks = new List<KeyValuePair<BundleInfo, UnityWebRequest>>();
for (int i = 0; i < list.Count; i++)
{
BundleInfo bundleInfo = list[i];
string fullname = BundleUtil.GetStorableDirectory() + bundleInfo.Filename;
UnityWebRequest www = new UnityWebRequest(GetAbsoluteUri(bundleInfo.Filename));
www.downloadHandler = new DownloadFileHandler(fullname);
#if UNITY_2018_1_OR_NEWER
www.SendWebRequest();
#else
www.Send();
#endif
tasks.Add(new KeyValuePair<BundleInfo, UnityWebRequest>(bundleInfo, www));
while (tasks.Count >= this.MaxTaskCount || (i == list.Count - 1 && tasks.Count > 0))
{
long tmpSize = 0;
for (int j = tasks.Count - 1; j >= 0; j--)
{
var task = tasks[j];
BundleInfo _bundleInfo = task.Key;
UnityWebRequest _www = task.Value;
if (!_www.isDone)
{
tmpSize += (long)Math.Max(0, _www.downloadedBytes);//the UnityWebRequest.downloadedProgress has a bug in android platform
continue;
}
progress.CompletedCount += 1;
tasks.RemoveAt(j);
downloadedSize += _bundleInfo.FileSize;
#if UNITY_2018_1_OR_NEWER
if (_www.isNetworkError)
#else
if (_www.isError)
#endif
{
promise.SetException(new Exception(_www.error));
if (log.IsErrorEnabled)
log.ErrorFormat("Downloads AssetBundle '{0}' failure from the address '{1}'.Code:{2} Reason:{3}", _bundleInfo.FullName, GetAbsoluteUri(_bundleInfo.Filename), _www.responseCode, _www.error);
_www.Dispose();
try
{
foreach (var kv in tasks)
{
kv.Value.Dispose();
}
}
catch (Exception) { }
yield break;
}
_www.Dispose();
}
progress.CompletedSize = downloadedSize + tmpSize;
promise.UpdateProgress(progress);
yield return null;
}
}
promise.SetResult(true);
}
}
Your answer

Follow this Question
Related Questions
About Implemented host name resolution with IPv6 in Unity 4.6.9 release-notes. 0 Answers
How do I make this GET request in Unity? 0 Answers
System.Threading.Thread use causing error on player exit in OSX 1 Answer
GET image over HTTPS with unvalidated SSL certificate and authenication 0 Answers
can we send data to GET api?,can we send data to get type api ? 0 Answers