Esempio n. 1
0
        /// <summary>
        /// 发送请求数据
        /// </summary>
        /// <param name="request">请求对象</param>
        /// <param name="postData">请求发送的字节数组</param>
        private void PostData(HttpWebRequest request, byte[] postData)
        {
            int             offset         = 0;
            int             sendBufferSize = bufferSize;
            int             remainBytes    = 0;
            Stream          stream         = request.GetRequestStream();
            UploadEventArgs args           = new UploadEventArgs();

            args.TotalBytes = postData.Length;
            while ((remainBytes = postData.Length - offset) > 0)
            {
                if (sendBufferSize > remainBytes)
                {
                    sendBufferSize = remainBytes;
                }
                stream.Write(postData, offset, sendBufferSize);
                offset += sendBufferSize;
                if (this.UploadProgressChanged != null)
                {
                    args.BytesSent = offset;
                    this.UploadProgressChanged(this, args);
                    if (args.Canceled)
                    {
                        break;
                    }
                }
            }
            stream.Close();
        }
Esempio n. 2
0
        ///<summary>
        ///向服务器发送请求
        ///</summary>
        ///<param name="URL">请求地址</param>
        ///<param name="method">POST或GET</param>
        ///<param name="showProgress">是否显示上传进度</param>
        private void SendRequestData(string URL, string method, bool showProgress)
        {
            clientSocket = new TcpClient();
            Uri URI = new Uri(URL);

            clientSocket.Connect(URI.Host, URI.Port);

            requestHeaders.Add("Host", URI.Host);
            byte[] request = GetRequestHeaders(method + " " + URI.PathAndQuery + " HTTP/1.1");
            clientSocket.Client.Send(request);

            //若有实体内容就发送它
            if (postStream != null)
            {
                byte[] buffer = new byte[SEND_BUFFER_SIZE];
                int    count  = 0;
                Stream sm     = clientSocket.GetStream();
                postStream.Position = 0;

                UploadEventArgs e = new UploadEventArgs();
                e.totalBytes = postStream.Length;
                System.Diagnostics.Stopwatch timer = new System.Diagnostics.Stopwatch();//计时器
                timer.Start();
                do
                {
                    //如果取消就推出
                    if (isCanceled)
                    {
                        break;
                    }

                    //读取要发送的数据
                    count = postStream.Read(buffer, 0, buffer.Length);
                    //发送到服务器
                    sm.Write(buffer, 0, count);

                    //是否显示进度
                    if (showProgress)
                    {
                        //触发事件
                        e.bytesSent   += count;
                        e.sendProgress = (double)e.bytesSent / (double)e.totalBytes;
                        double t = timer.ElapsedMilliseconds / 1000;
                        t           = t <= 0 ? 1 : t;
                        e.sendSpeed = (double)e.bytesSent / t;
                        if (UploadProgressChanged != null)
                        {
                            UploadProgressChanged(this, e);
                        }
                    }
                } while (count > 0);
                timer.Stop();
                postStream.Close();
                //postStream.Dispose();
                postStream = null;
            }//end if
        }