コード例 #1
0
ファイル: WebHelper.cs プロジェクト: kouweizhong/BizArk2
        private static void ProcessFileResponse(WebHelper web, ProcessResponseStreamEventArgs e, string fileName)
        {
            e.Handled = true;
            var    buffer    = new byte[web.Options.BufferSize];
            var    bytesRead = 0;
            int    read;
            Stream s;

            if (e.Response.ContentEncoding.ToLower().Contains("deflate"))
            {
                s = new DeflateStream(e.ResponseStream, CompressionMode.Decompress);
            }
            else if (e.Response.ContentEncoding.ToLower().Contains("gzip"))
            {
                s = new GZipStream(e.ResponseStream, CompressionMode.Decompress);
            }
            else
            {
                s = e.ResponseStream;
            }

            using (var fs = new FileStream(fileName, FileMode.CreateNew))
            {
                while ((read = s.Read(buffer, 0, buffer.Length)) > 0)
                {
                    fs.Write(buffer, 0, read);
                    bytesRead += read;
                    web.ReportResponseProgress(bytesRead);
                }
                fs.Flush();
            }

            e.Result = new FileInfo(fileName);
        }
コード例 #2
0
ファイル: WebHelper.cs プロジェクト: kouweizhong/BizArk2
 /// <summary>
 /// Raises the ProcessResponseStream event.
 /// </summary>
 /// <param name="e"></param>
 protected virtual void OnProcessResponseStream(ProcessResponseStreamEventArgs e)
 {
     if (ProcessResponseStream == null)
     {
         return;
     }
     ProcessResponseStream(this, e);
 }
コード例 #3
0
ファイル: WebHelper.cs プロジェクト: kouweizhong/BizArk2
        /// <summary>
        /// Processes the response from the server.
        /// </summary>
        /// <param name="response"></param>
        /// <returns></returns>
        protected virtual WebHelperResponse ProcessResponse(HttpWebResponse response)
        {
            //todo: check to see if this is set yet.
            mResponseContentLength = response.ContentLength;

            using (var responseStream = response.GetResponseStream())
            {
                var processArgs = new ProcessResponseStreamEventArgs(responseStream, response, Options.State);
                OnProcessResponseStream(processArgs);
                if (processArgs.Handled)
                {
                    return(new WebHelperResponse(processArgs.Result, response.ContentType, response.StatusCode, response.ContentEncoding, response.CharacterSet, Options));
                }

                Stream s;
                if (response.ContentEncoding.ToLower().Contains("deflate"))
                {
                    s = new DeflateStream(responseStream, CompressionMode.Decompress);
                }
                else if (response.ContentEncoding.ToLower().Contains("gzip"))
                {
                    s = new GZipStream(responseStream, CompressionMode.Decompress);
                }
                else
                {
                    s = responseStream;
                }

                var buffer    = new byte[Options.BufferSize];
                var bytesRead = 0;
                int read;
                using (var ms = new MemoryStream())
                {
                    while ((read = s.Read(buffer, 0, buffer.Length)) > 0)
                    {
                        if (CancellationPending)
                        {
                            return(null);
                        }

                        ms.Write(buffer, 0, read);
                        bytesRead += read;
                        ReportResponseProgress(bytesRead);
                    }
                    return(new WebHelperResponse(ms.ToArray(), response.ContentType, response.StatusCode, response.ContentEncoding, response.CharacterSet, Options));
                }
            }
        }