Esempio n. 1
0
        /// <summary>
        /// 发送异步请求,并获取处理回复
        /// </summary>
        /// <param name="asyncResult">异步状态</param>
        private void firePostDataRequest(IAsyncResult asyncResult)
        {
            this.startTime = DateTime.Now;
            HttpWebRequest request        = (HttpWebRequest)asyncResult.AsyncState;
            Stream         postDataStream = request.EndGetRequestStream(asyncResult);

            int totalBytes   = this.PostArgs.Data.Length;
            int writeTimes   = totalBytes / BUFFER_SIZE;
            int bytesWritten = 0;

            if (totalBytes % BUFFER_SIZE != 0)
            {
                writeTimes += 1;
            }
            for (int i = 0; i < writeTimes; i++)
            {
                //检查取消信号
                if (CancellationSignal != null && CancellationSignal())
                {
                    if (CompletionHandler != null)
                    {
                        CompletionHandler(ResponseInfo.cancelled(), "");
                    }
                    postDataStream.Close();
                    return;
                }
                int offset = i * BUFFER_SIZE;
                int size   = BUFFER_SIZE;
                if (i == writeTimes - 1)
                {
                    size = totalBytes - i * BUFFER_SIZE;
                }
                postDataStream.Write(this.PostArgs.Data, offset, size);
                bytesWritten += size;
                //请求进度处理
                if (ProgressHandler != null)
                {
                    ProgressHandler(bytesWritten, totalBytes);
                }
            }
            postDataStream.Flush();
            postDataStream.Close();
            request.BeginGetResponse(new AsyncCallback(handleResponse), request);
        }
Esempio n. 2
0
        /// <summary>
        /// 发送异步请求,并获取处理回复
        /// </summary>
        /// <param name="asyncResult">异步状态</param>
        private void fireMultipartPostRequest(IAsyncResult asyncResult)
        {
            this.startTime = DateTime.Now;
            HttpWebRequest request        = (HttpWebRequest)asyncResult.AsyncState;
            Stream         postDataStream = request.EndGetRequestStream(asyncResult);

            int bytesWritten = 0;
            int totalBytes   = (int)postDataMemoryStream.Length;
            int writeTimes   = totalBytes / BUFFER_SIZE;

            if (totalBytes % BUFFER_SIZE != 0)
            {
                writeTimes += 1;
            }
            postDataMemoryStream.Seek(0, SeekOrigin.Begin);
            int memNumRead = 0;

            byte[] memBuffer = new byte[BUFFER_SIZE];
            while ((memNumRead = postDataMemoryStream.Read(memBuffer, 0, memBuffer.Length)) != 0)
            {
                //检查取消信号
                if (this.CancellationSignal != null && this.CancellationSignal())
                {
                    if (this.CompletionHandler != null)
                    {
                        this.CompletionHandler(ResponseInfo.cancelled(), "");
                    }
                    postDataStream.Close();
                    return;
                }
                postDataStream.Write(memBuffer, 0, memNumRead);
                bytesWritten += memNumRead;
                //处理进度
                if (ProgressHandler != null)
                {
                    ProgressHandler(bytesWritten, totalBytes);
                }
            }
            postDataMemoryStream.Close();
            postDataStream.Flush();
            postDataStream.Close();
            request.BeginGetResponse(new AsyncCallback(handleResponse), request);
        }