C#HttpWebRequest块中的POST数据

时间:2014-03-05 09:35:45

标签: c# windows-phone-7 windows-phone-8 google-drive-api bytearray

字节数组的最大长度是多少?我正在尝试创建一个长度为 551858003 的字节数组。我创建了约526 MB的zip文件。但它给了我错误Out of memory exception我正在google drive上传文件。

这里我尝试了一些代码。我正在使用以下代码读取zip文件的字节。

 byte[] FileByteArray = null;

                using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    if (storage.FileExists(fileName))
                    { 
                        using (IsolatedStorageFileStream fileStream = storage.OpenFile(fileName,FileMode.Open,FileAccess.Read))
                        {
                            fileStream.Flush();
                            fileStream.Position = 0;
                            long len = fileStream.Length;
                            FileByteArray = new byte[len];
                            fileStream.Read(FileByteArray, 0, FileByteArray.Length);

                            //using (BinaryReader binReader = new BinaryReader(fileStream))
                            //{
                            //    Int32 Filelength = Convert.ToInt32(fileStream.Length);
                            //    FileByteArray = binReader.ReadBytes(Filelength);
                            //}
                            fileStream.Flush();
                            fileStream.Dispose();
                            fileStream.Close();
                        }
                    }
                }

如何解决此问题?我在上传大文件时收到OutOfMemoryException。我上传大约100MB左右。

以下是我在chunk

中发送图片的方法
 public void Images_ChunkRequst(string uploadURL, byte[] FileByteArray, int startIndex)
    {
        try
        {
            int chunkSize = 256 * 1024 * 2;
            int totalChunks = (int)Math.Ceiling((double)FileByteArray.Length / chunkSize);
            int endIndex = (int)(startIndex + chunkSize > FileByteArray.Length ? FileByteArray.Length : startIndex + chunkSize);
            int length = endIndex - startIndex;

            if (i < totalChunks)
            {
                 CollectionIP = CheckInternet.Find();
                if (CollectionIP.Count == 2 && DeviceNetworkInformation.IsWiFiEnabled)
                    NetworkIPaddress = IPAddress.Parse(CollectionIP[1]).ToString();
                else if (CollectionIP.Count > 0 && DeviceNetworkInformation.IsWiFiEnabled)
                    NetworkIPaddress = IPAddress.Parse(CollectionIP[0]).ToString();

                if (!string.IsNullOrEmpty(NetworkIPaddress))
                {
                    i = i + 1;
                    var request = WebRequest.Create(uploadURL) as HttpWebRequest;
                    request.Method = "PUT";
                    request.Headers["Authorization"] = string.Format("Bearer {0} ", AccessToken);
                    request.ContentType = "application/zip";
                    request.ContentLength = length;
                    request.Headers["Content-Range"] = "bytes " + startIndex + "-" + (endIndex - 1) + "/" + FileByteArray.Length;                        
                    request.AllowWriteStreamBuffering = false;

                    request.BeginGetRequestStream(arPut =>
                    {
                        var request1 = (HttpWebRequest)arPut.AsyncState;
                        using (var dataStream = request1.EndGetRequestStream(arPut))
                        {
                            //getting exception here
                            dataStream.Write(FileByteArray.Skip(startIndex).Take(endIndex).ToArray(), 0, FileByteArray.Take(length).Count());                                
                            dataStream.Flush();
                            dataStream.Dispose();
                            dataStream.Close();
                        }
                        request1.BeginGetResponse(aPut =>
                        {
                            var request2 = (HttpWebRequest)aPut.AsyncState;
                            WebRequest.RegisterPrefix("http://", WebRequestCreator.ClientHttp);
                            WebRequest.RegisterPrefix("https://", WebRequestCreator.ClientHttp);
                            var response = (HttpWebResponse)request2.EndGetResponse(aPut);

                            if (response.StatusCode.ToString() == "308") // Resume Incomplete
                            {
                                response.Dispose();
                                response.Close();

                                string packet = response.Headers["Range"];
                                string[] rng = packet.Remove(0, 6).Split('-');
                                Images_ChunkRequst(uploadURL, FileByteArray, Convert.ToInt32(rng[1]) + 1);
                            }
                            else if (response.StatusCode.ToString() == "500") //Internal Server Error
                            {
                                i = i - 1;
                                response.Dispose();
                                response.Close();
                                Images_ChunkRequst(uploadURL, FileByteArray, startIndex);
                            }
                            else if (response.StatusCode.ToString() == "502") //Bad Gateway
                            {
                                i = i - 1;
                                response.Dispose();
                                response.Close();
                                Images_ChunkRequst(uploadURL, FileByteArray, startIndex);
                            }
                            else if (response.StatusCode.ToString() == "503") //Service Unavailable
                            {
                                i = i - 1;
                                response.Dispose();
                                response.Close();
                                Images_ChunkRequst(uploadURL, FileByteArray, startIndex);
                            }
                            else if (response.StatusCode.ToString() == "504") //Gateway Timeout
                            {
                                i = i - 1;

                                response.Dispose();
                                response.Close();
                                Images_ChunkRequst(uploadURL, FileByteArray, startIndex);
                            }
                            else if (response.StatusCode == HttpStatusCode.NotFound) //Not Found
                            {
                                i = i - 1;

                                response.Dispose();
                                response.Close();
                                Images_ChunkRequst(uploadURL, FileByteArray, startIndex);
                            }
                            else if (response.StatusCode == HttpStatusCode.OK) // upload complete.
                            {
                                Dispatcher.BeginInvoke(() =>
                                {
                                    i = 0;// i must be 0 after each upload success
                                    imgPass_Images.Visibility = Visibility.Visible;
                                    this.LayoutRoot.IsHitTestVisible = true;
                                    SystemTray.ProgressIndicator.IsIndeterminate = false;
                                    SystemTray.IsVisible = false;
                                    GetNextAutoOrder();
                                });
                            }
                        }, request1);
                    }, request);
                }
                else
                {
                    this.LayoutRoot.IsHitTestVisible = true;
                    SystemTray.ProgressIndicator.IsIndeterminate = false;
                    SystemTray.IsVisible = false;
                    MessageBox.Show("Please check your internet connection.");
                }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error occured. Trying to send failed package.");
            Images_ChunkRequst(uploadURL, FileByteArray, startIndex);
        }
    }

我在dataStream.Write(FileByteArray.Skip(startIndex).Take(endIndex).ToArray(), 0, FileByteArray.Take(length).Count());处遇到异常。我已经尝试了很多来解决这个问题。有人可以帮帮我吗?我收到此异常Insufficient memory to continue the execution of the program

3 个答案:

答案 0 :(得分:1)

System.Array的当前实现对其所有内部计数器等使用Int32,因此理论上最大元素数为Int32.MaxValue。所以问题出在其他地方。

答案 1 :(得分:0)

这与Array.Length无关。你正在耗尽进程内存。你应该以块的形式进行分配。

答案 2 :(得分:0)

这可能不是解决方案,但我发布的代码存在一些问题:

                    using (IsolatedStorageFileStream fileStream = storage.OpenFile(fileName,FileMode.Open,FileAccess.Read))
                    {
                        fileStream.Flush();
                        fileStream.Position = 0;
                        long len = fileStream.Length;
                        FileByteArray = new byte[len];
                        fileStream.Read(FileByteArray, 0, FileByteArray.Length);

                        //using (BinaryReader binReader = new BinaryReader(fileStream))
                        //{
                        //    Int32 Filelength = Convert.ToInt32(fileStream.Length);
                        //    FileByteArray = binReader.ReadBytes(Filelength);
                        //}
                        fileStream.Flush();
                        fileStream.Dispose();
                        fileStream.Close();
                    }
  • fileStream.Flush()如果强制将缓冲区写入磁盘。阅读
  • 不需要
  • fileStream.Close()会失败,因为在它之前调用Dispose()
  • fileStream.Dispose()和fileStream.Close()不是必需的,因为它应该由using()语句自动处理
  • fileStream.Read(array,start,lenght)返回实际读取的字节数的整数,因此它并不总是与实际长度匹配。你应该继续阅读,直到返回零。这就是我建议遵循的原因:

而不是将FileByteArray创建为流的大小,也可以在chucks中执行,也许可以将结果写入内存流(或者如果它支持发送chucks,甚至可以更好地将其直接发送到API。)所以基本上:

创建一个类似1024的字节数组(如果你想从缓冲区读取更大的数据,可能会更大 - 但要避免太大)

循环阅读:

byte[] buffer = new byte[1024];
int read = 0;
while((read = fileStream.Read(buffer, 0, buffer.Length)) > 0)
     // write buffer[0:read] to memory location or temp file on disk (avoid Flushing for each write);