При тестировании этого метода в консольном приложении Visual Studio результат положительный.
При попытке использования этого метода в тестовом проекте в Unity происходит непонятное "зависание", когда выполнение доходит до оператора:
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()),
а затем происходит зависание, причину которого я никак не могу выяснить даже при установке точки останова (breakpoint) на этом операторе во время отладки.
Причем этот блок using у меня обрамлен конструкцией try-catch с обработкой соответствующих исключений, но во время выполнения никакого исключения не возникает.
Вот используемый мною класс с загрузчиком файлов по HTTP с возможностью докачки при разрыве соединения.
Синтаксис:
Используется csharp
namespace SceneDownloader
{
using UnityEngine;
using System;
using System.IO;
using System.Net;
public class FileDownloader : MonoBehaviour
{
static int bufferSize = 1024 * 1000;
static long offset = 0L;
public static void DownloadSingleFile(string sourceURL, string destinationPath)
{
try
{
if (File.Exists(destinationPath))
{
FileInfo fileInfo = new FileInfo(destinationPath);
offset = fileInfo.Length;
}
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(sourceURL);
request.AddRange((int)offset);
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
Stream source = response.GetResponseStream();
if (source != null)
{
byte[] buffer = new byte[bufferSize];
using (FileStream fileStream = new FileStream(destinationPath, FileMode.OpenOrCreate, FileAccess.Write))
{
fileStream.Position = offset;
while (true)
{
int readed = source.Read(buffer, 0, buffer.Length);
if (readed == 0) break;
fileStream.Write(buffer, 0, readed);
} // while (true)
} // using (FileStream fileStream
} // if (source != null)
} // using (HttpWebResponse response)
} // try
catch (WebException ex)
{
Debug.Log("Trying to connect (check your connection)...");
DownloadSingleFile(sourceURL, destinationPath);
} // catch (WebException ex)
catch (IOException ex)
{
Debug.Log("Trying to connect (check your connection)...");
DownloadSingleFile(sourceURL, destinationPath);
} // catch (IOException ex)
catch (Exception ex)
{
Debug.Log("Trying to connect (check your connection)...");
DownloadSingleFile(sourceURL, destinationPath);
} // catch (Exception ex)
} // public static void DownloadSingleFile
} // public class FileDownloader
} // namespace SceneDownloader
{
using UnityEngine;
using System;
using System.IO;
using System.Net;
public class FileDownloader : MonoBehaviour
{
static int bufferSize = 1024 * 1000;
static long offset = 0L;
public static void DownloadSingleFile(string sourceURL, string destinationPath)
{
try
{
if (File.Exists(destinationPath))
{
FileInfo fileInfo = new FileInfo(destinationPath);
offset = fileInfo.Length;
}
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(sourceURL);
request.AddRange((int)offset);
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
Stream source = response.GetResponseStream();
if (source != null)
{
byte[] buffer = new byte[bufferSize];
using (FileStream fileStream = new FileStream(destinationPath, FileMode.OpenOrCreate, FileAccess.Write))
{
fileStream.Position = offset;
while (true)
{
int readed = source.Read(buffer, 0, buffer.Length);
if (readed == 0) break;
fileStream.Write(buffer, 0, readed);
} // while (true)
} // using (FileStream fileStream
} // if (source != null)
} // using (HttpWebResponse response)
} // try
catch (WebException ex)
{
Debug.Log("Trying to connect (check your connection)...");
DownloadSingleFile(sourceURL, destinationPath);
} // catch (WebException ex)
catch (IOException ex)
{
Debug.Log("Trying to connect (check your connection)...");
DownloadSingleFile(sourceURL, destinationPath);
} // catch (IOException ex)
catch (Exception ex)
{
Debug.Log("Trying to connect (check your connection)...");
DownloadSingleFile(sourceURL, destinationPath);
} // catch (Exception ex)
} // public static void DownloadSingleFile
} // public class FileDownloader
} // namespace SceneDownloader
Скажите, по какой причине может происходить это непонятное "зависание" при вызове метода загрузки файла по HTTP из скрипта в Unity?
Я, может быть, повторюсь, но этот же код отлично работает при его тестировании из консольного приложения в Visual Studio.
Как мне исправить возникшую проблему?