Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 下载有时是不完整的,没有任何错误或异常_C# - Fatal编程技术网

C# 下载有时是不完整的,没有任何错误或异常

C# 下载有时是不完整的,没有任何错误或异常,c#,C#,这是我用来从服务器下载文件的代码。有时文件没有完全下载。例如,如果我下载一个200MB的文件,它只下载50MB,并且停止,没有给出任何错误或异常。大多数情况下,大约80%的时间,文件是完全下载的。我的代码可能有什么错误?缺少多少字节?如果小于缓冲区大小,那么我猜您的问题是没有正确关闭/刷新strLocal流。是downBuffer realy 10240,还是只是一个写入错误。如果您将其更改为1024谢谢您的回复。DownBuffer是10240,我只是增加了缓冲区大小以减少下载时间,甚至尝试了

这是我用来从服务器下载文件的代码。有时文件没有完全下载。例如,如果我下载一个200MB的文件,它只下载50MB,并且停止,没有给出任何错误或异常。大多数情况下,大约80%的时间,文件是完全下载的。我的代码可能有什么错误?

缺少多少字节?如果小于缓冲区大小,那么我猜您的问题是没有正确关闭/刷新strLocal流。

是downBuffer realy 10240,还是只是一个写入错误。如果您将其更改为1024谢谢您的回复。DownBuffer是10240,我只是增加了缓冲区大小以减少下载时间,甚至尝试了缓冲区大小为1024的情况,结果也是一样的。我发现strResponse.Read-in while循环条件返回的bytesSize为0,即使尚未到达文件结尾。如何处理?不加评论地投反对票。是的,这很有道理。
        // It will store the current number of bytes we retrieved from the server
        int bytesSize = 0;
        // A buffer for storing and writing the data retrieved from the server
        byte[] downBuffer = new byte[10240];
        bool exceptionOccured = false;
        HttpWebRequest webRequest = null;
        try
        {


// Create a request to the file we are downloading
webRequest = (HttpWebRequest)WebRequest.Create(urlAndChecksum.FILEURL);
webRequest.Timeout = 120000;
webRequest.ReadWriteTimeout = 300000;
webRequest.ConnectionGroupName = mediaName;
// Set default authentication for retrieving the file
//webRequest.Credentials = new NetworkCredential(GlobalVariables.username, GlobalVariables.password);
webRequest.UseDefaultCredentials = true;

// Retrieve the response from the server
using (HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse())
{
    // Open the URL for download 
    strResponse = webResponse.GetResponseStream();

    // Create a new file stream where we will be saving the data (local drive)
    strLocal = File.Create(destFilePath);

    // Loop through the buffer until the buffer is empty
    while ((bytesSize = strResponse.Read(downBuffer, 0, downBuffer.Length)) > 0)
    {
        if (isPaused)
        {
            Logger.WriteToLog(mediaName);
            waitRun_m.WaitOne();
        }

        if (isCanceled)
        {
            Logger.WriteToLog("Before Cancel :: Concurrent connections = " + webRequest.ServicePoint.CurrentConnections);

            webResponse.Close();
            //webRequest.Abort();
            webRequest.ServicePoint.CloseConnectionGroup(mediaName);
            Logger.WriteToLog("After Cancel :: Concurrent connections = " + webRequest.ServicePoint.CurrentConnections);
            return;
        }

        strLocal.Write(downBuffer, 0, bytesSize);
        // Invoke the method that updates the form's label and progress bar
        UpdateProgessCallback(mediaName, bytesSize);
    };
}

 catch (Exception ex)
        {
            exceptionOccured = true;
            MessageBox.Show(ex.Message, "Cadence Download Manager", MessageBoxButton.OK, MessageBoxImage.Error);
            isExceptionOccured = true;
            StopAllDownloads(Thread.CurrentThread, ex.Message, HttpStatusCode.NotFound);
        }
        finally
        {
            if (!isExceptionOccured)
            {
                if (strResponse != null)
                    strResponse.Close();
                if (strLocal != null)
                    strLocal.Close();
}
}