예제 #1
0
        public void TransferredProgress(int bytesTransferred)
        {
            if (callback == null)
            {
                return;
            }

            // Invoke the progress callback only if bytes read > 0
            if (bytesTransferred > 0)
            {
                totalBytesTransferred     += bytesTransferred;
                totalIncrementTransferred += bytesTransferred;

                if (totalIncrementTransferred >= this.progressUpdateInterval ||
                    totalBytesTransferred == contentLength)
                {
                    StreamEventUtils.InvokeInBackground(
                        callback,
                        new StreamTransferProgressArgs(totalIncrementTransferred, totalBytesTransferred, contentLength),
                        sender);
                    totalIncrementTransferred = 0;
                }
            }
        }
예제 #2
0
        public HttpWebRequest GetWebRequest(HttpRequest request)
        {
            HttpWebRequest httpWebRequest = null;

            httpWebRequest             = (HttpWebRequest)WebRequest.Create(request.Url);
            httpWebRequest.Method      = request.Method.ToString();
            httpWebRequest.KeepAlive   = true;
            httpWebRequest.Timeout     = _timeout;
            httpWebRequest.SendChunked = request.UseChunkedEncoding;
            if (!request.UseChunkedEncoding)
            {
                httpWebRequest.AllowWriteStreamBuffering = false;
            }
            var originalStream = request.BodyContent;
            var callback       = request.StreamTransferProgress;

            if (callback != null)
            {
                originalStream      = StreamEventUtils.SetupProgressListeners(originalStream, originalStream.Length, 0, 200, request, callback);
                request.BodyContent = originalStream;
            }
            if (request.Headers.ContainsKey("Accept"))
            {
                httpWebRequest.Accept = DictionaryUtil.Pop(request.Headers, "Accept");
            }
            if (request.Headers.ContainsKey("Date"))
            {
                httpWebRequest.Date = Convert.ToDateTime(DictionaryUtil.Pop(request.Headers, "Date"));
            }

            foreach (var header in request.Headers)
            {
                if (header.Key.Equals("Content-Length"))
                {
                    httpWebRequest.ContentLength = long.Parse(header.Value);
                    continue;
                }
                if (header.Key.Equals("Content-Type"))
                {
                    httpWebRequest.ContentType = header.Value;
                    continue;
                }

                httpWebRequest.Headers.Add(header.Key, header.Value);
            }

            if ((request.Method == MethodType.POST || request.Method == MethodType.PUT) && request.BodyContent != null)
            {
                using (Stream stream = httpWebRequest.GetRequestStream())
                {
                    if (httpWebRequest.SendChunked)
                    {
                        IOUtils.WriteTo(request.BodyContent, stream, request.BodyContent.Length);
                    }
                    else
                    {
                        IOUtils.WriteTo(request.BodyContent, stream);
                    }
                }
            }


            return(httpWebRequest);
        }