我開發C#應用程序,其中我從服務器下載machine.It包(zip文件)被正確下載,但最近我們的包數據已經得到了一些改變,其通過使用C#是柔性application.And我們正在下載到C盤或D盤。下載使用C#代碼
現在有了新的軟件包我面臨的一些問題, 無法讀取傳輸連接的數據:由於系統缺乏足夠的緩衝區空間或者因爲隊列已滿無法執行套接字上的操作。
我的代碼是下面
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),我得到誤差 無法讀取從傳輸連接數據:套接字上的操作不能因爲系統執行缺乏足夠的緩衝空間或者隊列已滿。
請幫我在這,怎麼我不應該也更改代碼。
在此先感謝 桑吉塔
到底有多大這個文件?也許你正在耗盡你的可用內存? – cdhowie
的文件大小爲71 KB – user703526
看一看[這個博客帖子(http://blogs.msdn.com/b/sql_protocols/archive/2009/03/09/understanding-the-error-an-operation-因爲系統缺乏足夠的緩衝區空間或因爲隊列是full.aspx)和[這個答案](http: //stackoverflow.com/questions/557879/why-am-i-getting-this-socketexception-in-my-c-service/557902#557902),看看它是否有幫助。 – svick