我正在开发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)中,我收到错误 无法从传输连接读取数据:无法执行对套接字的操作,因为系统缺少足够的缓冲区空间或队列已满。
请帮助我,因为我不应该更改代码。
提前致谢 桑吉塔
答案 0 :(得分: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如果您没有看到评论中提供的链接。