コード例 #1
0
        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);
            }
        }
コード例 #2
0
        private static void SetContent(HttpWebRequest webRequest,
                                       HttpRequest httpRequest,
                                       ObsConfig obsConfig)
        {
            Stream data = httpRequest.Content;

            if (data == null)
            {
                data = new MemoryStream();
            }

            long userSetContentLength = -1;

            if (httpRequest.Headers.ContainsKey(Constants.CommonHeaders.ContentLength))
            {
                userSetContentLength = long.Parse(httpRequest.Headers[Constants.CommonHeaders.ContentLength]);
            }

            if (userSetContentLength >= 0)
            {
                webRequest.ContentLength = userSetContentLength;
                if (webRequest.ContentLength > Constants.DefaultStreamBufferThreshold)
                {
                    webRequest.AllowWriteStreamBuffering = false;
                }
            }
            else
            {
                webRequest.SendChunked = true;
                webRequest.AllowWriteStreamBuffering = false;
            }

            using (Stream requestStream = webRequest.GetRequestStream())
            {
                if (!webRequest.SendChunked)
                {
                    CommonUtil.WriteTo(data, requestStream, webRequest.ContentLength, obsConfig.BufferSize);
                }
                else
                {
                    CommonUtil.WriteTo(data, requestStream, obsConfig.BufferSize);
                }
            }
        }