【问题标题】:Download using c# code使用c#代码下载
【发布时间】:2011-09-06 14:53:54
【问题描述】:

我正在开发 c# 应用程序,我正在从服务器机器下载包(zip 文件)。它正在正确下载,但最近我们的包数据发生了一些变化,即 flex 应用程序。通过使用 c#,我们正在下载它放入c盘或d盘。

现在有了新包装,我面临一些问题 无法从传输连接读取数据:无法对套接字执行操作,因为系统缺少足够的缓冲区空间或队列已满。

我的代码在下面

byte[] packageData = null;
packageData = touchServerClient.DownloadFile("/packages/" + this.PackageName);
public byte[] DownloadFile(string url)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(remoteSite.Url + url);
            try
            {
                request.Method = "GET";
                request.KeepAlive = false;                

                request.CookieContainer = new CookieContainer();
                if (this.Cookies != null && this.Cookies.Count > 0)
                    request.CookieContainer.Add(this.Cookies);

                HttpWebResponse webResponse = (HttpWebResponse)request.GetResponse();                

                // Console.WriteLine(response.StatusDescription);

                Stream responseStream = webResponse.GetResponseStream();

                int contentLength = Convert.ToInt32(webResponse.ContentLength);
                byte[] fileData = StreamToByteArray(responseStream, contentLength);                
                return fileData;
            }



public static byte[] StreamToByteArray(Stream stream, int initialLength)
        {
            // If we've been passed an unhelpful initial length, just
            // use 32K.
            if (initialLength < 1)
            {
                initialLength = 32768;
            }

            byte[] buffer = new byte[initialLength];
            int read = 0;

            int chunk;
            while ((chunk = stream.Read(buffer, read, buffer.Length - read)) > 0)
            {
                read += chunk;

                // If we've reached the end of our buffer, check to see if there's
                // any more information
                if (read == buffer.Length)
                {
                    int nextByte = stream.ReadByte();

                    // End of stream? If so, we're done
                    if (nextByte == -1)
                    {
                        return buffer;
                    }

                    // Nope. Resize the buffer, put in the byte we've just
                    // read, and continue
                    byte[] newBuffer = new byte[buffer.Length * 2];
                    Array.Copy(buffer, newBuffer, buffer.Length);
                    newBuffer[read] = (byte)nextByte;
                    buffer = newBuffer;
                    read++;
                }
            }
            // Buffer is now too big. Shrink it.
            byte[] ret = new byte[read];
            Array.Copy(buffer, ret, read);
            return ret;
        }

在上面的函数(StreamToByteArray)中,我收到错误消息 无法从传输连接读取数据:无法对套接字执行操作,因为系统缺少足够的缓冲区空间或队列已满。

请帮助我,因为我也不应该更改代码。

提前致谢 桑吉塔

【问题讨论】:

标签: c# httpwebrequest


【解决方案1】:

有几件事可以尝试:

  • 将流处理封装在 using 语句中。

这将清理该资源。

using (Stream responseStream = webResponse.GetResponseStream())
{
    int contentLength = Convert.ToInt32(webResponse.ContentLength);
    byte[] fileData = StreamToByteArray(responseStream, contentLength);
    return fileData;        
}
  • 确保没有其他大量内存进程在同一个机器上运行。特别是当他们进行 Socket 绑定调用时。

  • 尝试提高MaxUserPort 注册表值的值。 Here is the article 如果您没有看到 cmets 中提供的链接。

【讨论】:

  • 我认为你是对的,不处理流是这里的问题。
  • 是包文件大小问题,60mb以上。我所有的旧包都在50,000kb以下。正在下载,没有任何失败。
  • 我认为是大尺寸的问题。
  • 嗯...我能提出的唯一其他建议是以更小的块读取您的数据。以较小的读取量循环几次,您不会看到任何实际性能受到影响。
猜你喜欢
  • 2021-03-19
  • 2012-09-03
  • 2015-01-11
  • 2011-01-21
  • 2013-10-09
  • 2018-08-09
  • 1970-01-01
  • 2014-03-04
  • 1970-01-01
相关资源
最近更新 更多