private void EndGetRequestStream(IAsyncResult ar) { HttpObsAsyncResult asyncResult = ar.AsyncState as HttpObsAsyncResult; HttpWebRequest webRequest = asyncResult.HttpWebRequest; ObsConfig obsConfig = asyncResult.HttpContext.ObsConfig; Stream data = asyncResult.HttpRequest.Content; if (data == null) { data = new MemoryStream(); } try { using (Stream requestStream = webRequest.EndGetRequestStream(ar)) { ObsCallback callback = delegate() { asyncResult.IsTimeout = false; }; if (!webRequest.SendChunked) { CommonUtil.WriteTo(data, requestStream, webRequest.ContentLength, obsConfig.BufferSize, callback); } else { CommonUtil.WriteTo(data, requestStream, obsConfig.BufferSize, callback); } } asyncResult.Continue(this.EndGetResponse); } catch (Exception e) { asyncResult.Abort(e); } }
public static void WriteTo(Stream src, Stream dest, int bufferSize, ObsCallback callback) { DateTime reqTime = DateTime.Now; byte[] buffer = new byte[bufferSize]; int bytesRead; while ((bytesRead = src.Read(buffer, 0, buffer.Length)) > 0) { dest.Write(buffer, 0, bytesRead); callback?.Invoke(); } dest.Flush(); if (LoggerMgr.IsInfoEnabled) { LoggerMgr.Info(string.Format("Write http request stream end, cost {0} ms", (DateTime.Now.Ticks - reqTime.Ticks) / 10000)); } }
public static long WriteTo(Stream orignStream, Stream destStream, long totalSize, int bufferSize, ObsCallback callback) { DateTime reqTime = DateTime.Now; byte[] buffer = new byte[bufferSize]; long alreadyRead = 0; while (alreadyRead < totalSize) { int readSize = orignStream.Read(buffer, 0, bufferSize); if (readSize <= 0) { break; } if (alreadyRead + readSize > totalSize) { readSize = (int)(totalSize - alreadyRead); } alreadyRead += readSize; destStream.Write(buffer, 0, readSize); callback?.Invoke(); } destStream.Flush(); if (LoggerMgr.IsInfoEnabled) { LoggerMgr.Info(string.Format("Write http request stream end, cost {0} ms", (DateTime.Now.Ticks - reqTime.Ticks) / 10000)); } return(alreadyRead); }