void NetworkReadCallback(IAsyncResult ar) { var count = 0; try { count = AsyncData.Stream.EndRead(ar); if (count > 0) { ResultStream.Write(_buffer, 0, count); } } catch (Exception exception) { AsyncData.Exception = exception; CompleteCallback(); return; } if (count == 0) { ProcessFinalResponse(); ResultStream.Seek(0, SeekOrigin.Begin); CompleteCallback(); } else { AsyncData.Stream.BeginRead(_buffer, 0, _buffer.Length, NetworkReadCallback, this); } }
public void SubmitToStream(BuiltinReportDescriptor descriptor) { /* load the master template */ var raw = LoadTemplate("builtinreport.ent", descriptor.m); /* do we need the JS libraries of amline? */ if (NeedsAMJSLines) { var flashscript = LoadTemplate("am_embed_js.html", descriptor.m); raw.Set("flashscript", flashscript.ToString()); } else { raw.Set("flashscript", String.Empty); /* added [dlatikay 20120202] */ } /* perform the replacements */ raw.Set("title", descriptor.ReportPageTitle); raw.Set("dqp", descriptor.DQP); /* added [dlatikay 20120209] */ raw.Set("onloadaction", descriptor.Outputformat == FileType.ft_html ? "window.print();" : String.Empty); /* added [dlatikay 20120209] */ raw.Set("header", header == null ? String.Empty : header.ToString()); raw.Set("meta", meta == null ? String.Empty : meta.ToString()); var compositecontent = new StringBuilder(); var partcounter = 0; foreach (var part in parts) { compositecontent.AppendFormat("{1}{0}{2}", part.ToString(), (((++partcounter) <= 1) ? String.Empty : "<!-- section ruler -->"), Environment.NewLine); } raw.Set("rendition", compositecontent.ToString()); raw.Set("footer", String.Format("<span id=\"builtinreport_footer\">{0}</span>", HttpUtility.HtmlEncode(String.Format(descriptor.m(3252), descriptor.Systemname, descriptor.Systemversion, descriptor.ReportCreationTimestamp)))); /* Created using en-software SHERM v2.1 */ raw.Set("onafterprint", descriptor.Outputformat == dal.business.FileType.ft_pdf ? "JavaScript:window.location.href='about:blank';" : String.Empty); /* when we are rendering a pdf, we will print it to the postscript printer. therefore we need this to allow the silent host browser to detect when printing has finished */ /* [dlatikay 20111202] if we want html, we get html; * stream it out: */ if (descriptor.Outputformat == dal.business.FileType.ft_html) { byte[] html = System.Text.UTF8Encoding.UTF8.GetBytes(raw.ToString()); ResultStream.Write(html, 0, html.Length); } /* reset if can */ if (ResultStream.CanSeek) { ResultStream.Seek(0, SeekOrigin.Begin); } }
private void Start(int retryCount, Stream content = null) { _progressing = true; _progressWaitHandle.Reset(); int retry = 0; Task.Run(async() => { HttpRequestQueue.Current.Register(this); _queueWaitHandle.WaitOne(); RETRY: //重试标签 if (State == HttpRequestState.NothingSpecial) { EventArgs <HttpRequestStats> progressInfo = null; if (TransferProgressChanged != null) { progressInfo = new EventArgs <HttpRequestStats>(new HttpRequestStats()); } try { State = HttpRequestState.Connecting; if (TransferProgressChanged != null) { progressInfo.Value.Update(); TransferProgressChanged(this, progressInfo); } //数据上传过程,用于 Put 与 Post if (content != null && (Request.Method == "PUT" || Request.Method == "POST")) { using (var requestStream = await Request.GetRequestStreamAsync()) { State = HttpRequestState.Progressing; if (TransferProgressChanged != null) { progressInfo.Value.ProgressType = HttpRequestProgressType.Upload; progressInfo.Value.Update(); progressInfo.Value.TotalBytes = content.Length; TransferProgressChanged(this, progressInfo); byte[] buffer = new byte[TransferBufferSize]; int writeLength; while ((writeLength = content.Read(buffer, 0, buffer.Length)) != 0) { requestStream.Write(buffer, 0, writeLength); progressInfo.Value.Update(writeLength); TransferProgressChanged(this, progressInfo); } progressInfo = new EventArgs <HttpRequestStats>(new HttpRequestStats()); } else { content.CopyTo(requestStream); } } } //数据下载过程,用于 Post 与 Get if (Request.Method == "GET" || Request.Method == "POST") { Response = (HttpWebResponse)await Request.GetResponseAsync(); State = HttpRequestState.Progressing; #region 载过程 using (var stream = Response.GetResponseStream()) { State = HttpRequestState.Progressing; if (_customStream == null) { ResultStream = new MemoryStream((int)(Response.ContentLength > 0 ? Response.ContentLength : 10 * TransferBufferSize)); } if (TransferProgressChanged != null) { progressInfo.Value.ProgressType = HttpRequestProgressType.Download; progressInfo.Value.TotalBytes = Response.ContentLength > 0 ? Response.ContentLength : 0; byte[] buffer = new byte[TransferBufferSize]; long pos = 0; int readLength; if (ResultStream.CanSeek) { pos = ResultStream.Position; } while ((readLength = stream.Read(buffer, 0, buffer.Length)) != 0) { ResultStream.Write(buffer, 0, readLength); progressInfo.Value.Update(readLength); TransferProgressChanged(this, progressInfo); } if (ResultStream.CanSeek) { ResultStream.Position = pos; } } else { long pos = 0; if (ResultStream.CanSeek) { pos = ResultStream.Position; } stream.CopyTo(ResultStream); if (ResultStream.CanSeek) { ResultStream.Position = pos; } } } #endregion 载过程 } State = HttpRequestState.Completed; if (TransferProgressChanged != null) { progressInfo.Value.Update(); TransferProgressChanged(this, progressInfo); } _progressWaitHandle.Set(); _progressing = false; return; } catch (WebException webException) { Exception = webException; State = HttpRequestState.ErrorOccurred; if (TransferProgressChanged != null) { progressInfo.Value.Update(); TransferProgressChanged(this, progressInfo); } } catch (Exception ex) { Exception = ex; _progressWaitHandle.Set(); _progressing = false; throw; } } if (retry < retryCount) { retry++; RebuildRequest(); //重构请求 goto RETRY; //重试请求 } //取消等待 _progressWaitHandle.Set(); _progressing = false; }); }