locked
when HTTPClient get large file. app is forced terminated. RRS feed

  • 질문

  • when HTTPClient get large file. app is forced terminated. 

    getting files which size is below about 140 mega bytes are ok.

    when getting over 140 mega  bytes file, app is forced terminated without any exception.  this is my code


    --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

            private async Task<bool> AsyncRecvFile(TransInfo TransInfo)
            {
                string strUserAgent = m_Head.GetUserAgent();
                string strContentType = m_Head.GetContentType();
                HTTPClientHeadAuthor Author = m_Head.GetAuthorization();

                Stream ReadStream = null;

                try
                {
                    StorageFile File = TransInfo.GetFile();

                    if (File == null)
                    {
                        m_NotifyWriter.Write(new NotifyError(this.ToString(), "AsyncRecvFile::File is NULL"));
                        return false;
                    }

                    m_Client = new HttpClient();

                    if (TransInfo.GetSize() > 0)
                        m_Client.MaxResponseContentBufferSize = TransInfo.GetSize();

                    if (strUserAgent != null)
                        m_Client.DefaultRequestHeaders.Add("user-agent", strUserAgent);

                    if (Author != null)
                        m_Client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(Author.GetAuthorType(), Author.GetAuthorValue());

                    m_Client.Timeout = TimeSpan.FromSeconds(600);
                    m_Client.DefaultRequestHeaders.ExpectContinue = false;

                    ReadStream = await m_Client.GetStreamAsync(m_strURL);

                    int TotalLength = 0;
                   
                    using (Stream WriteStream = await File.OpenStreamForWriteAsync())
                    {
                        const int BufferSize = 0xFFFF;
                        byte[] ReadBuffer = new byte[BufferSize];

                        while (m_RunTask)
                        {
                            int ReadSize = ReadStream.Read(ReadBuffer, 0, BufferSize);

                            if (ReadSize <= 0)
                                break;

                            WriteStream.Write(ReadBuffer, 0, ReadSize);

                            TotalLength += ReadSize;
                        }

                        WriteStream.Flush();
                        WriteStream.Dispose();
                    }

                    m_Client.Dispose();
                    ReadStream.Dispose();

                    return true;
                }
                catch (Exception e)
                {
                    if (m_Client != null)
                    {
                        m_Client.Dispose();
                        m_Client = null;
                    }

                    if (ReadStream != null)
                    {
                        ReadStream.Dispose();
                        ReadStream = null;
                    }

                    Debug.WriteLine("AsyncRecvFile::Exception - " + e.ToString());

                    return false;
                }
            }
            

    --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    2014년 9월 4일 목요일 오전 9:35