///<summary> ///将网络流保存到指定流 ///</summary> ///<param name="toStream">保存位置</param> ///<param name="needProgress">是否显示进度</param> private void SaveNetworkStream(Stream toStream, bool showProgress) { //获取要保存的网络流 NetworkStream NetStream = clientSocket.GetStream(); byte[] buffer = new byte[RECEIVE_BUFFER_SIZE]; int count = 0, startIndex = 0; MemoryStream ms = new MemoryStream(); for (int i = 0; i < 3; i++) { count = NetStream.Read(buffer, 0, 500); ms.Write(buffer, 0, count); } if (ms.Length == 0) { NetStream.Close(); throw new Exception("远程服务器没有响应"); } buffer = ms.GetBuffer(); count = (int)ms.Length; GetResponseHeader(buffer, out startIndex);//分析响应,获取响应头和响应实体 count -= startIndex; toStream.Write(buffer, startIndex, count); DownloadEventArgs e = new DownloadEventArgs(); if (responseHeaders["Content-Length"] != null) { e.totalBytes = long.Parse(responseHeaders["Content-Length"]); } else { e.totalBytes = -1; } //启动计时器 System.Diagnostics.Stopwatch timer = new System.Diagnostics.Stopwatch(); timer.Start(); do { //如果取消就推出 if (isCanceled) { break; } //显示下载进度 if (showProgress) { e.bytesReceived += count; e.ReceiveProgress = (double)e.bytesReceived / (double)e.totalBytes; byte[] tempBuffer = new byte[count]; Array.Copy(buffer, startIndex, tempBuffer, 0, count); e.receivedBuffer = tempBuffer; double t = (timer.ElapsedMilliseconds + 0.1) / 1000; e.receiveSpeed = (double)e.bytesReceived / t; startIndex = 0; if (DownloadProgressChanged != null) { DownloadProgressChanged(this, e); } } //读取网路数据到缓冲区 count = NetStream.Read(buffer, 0, buffer.Length); //将缓存区数据保存到指定流 toStream.Write(buffer, 0, count); } while (count > 0); timer.Stop();//关闭计时器 if (responseHeaders["Content-Length"] != null) { toStream.SetLength(long.Parse(responseHeaders["Content-Length"])); } //else //{ // toStream.SetLength(toStream.Length); // responseHeaders.Add("Content-Length", toStream.Length.ToString());//添加响应标头 //} toStream.Position = 0; //关闭网络流和网络连接 NetStream.Close(); clientSocket.Close(); }
/// <summary> /// 读取请求返回的数据 /// </summary> /// <param name="request">请求对象</param> /// <returns></returns> private byte[] GetData(HttpWebRequest request) { HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream stream = response.GetResponseStream(); responseHeaders = response.Headers; SaveCookiesToDisk(); DownloadEventArgs args = new DownloadEventArgs(); if (responseHeaders[HttpResponseHeader.ContentLength] != null) { args.TotalBytes = Convert.ToInt32(responseHeaders[HttpResponseHeader.ContentLength]); } MemoryStream ms = new MemoryStream(); int count = 0; byte[] buf = new byte[bufferSize]; while ((count = stream.Read(buf, 0, buf.Length)) > 0) { ms.Write(buf, 0, count); if (this.DownloadProgressChanged != null) { args.BytesReceived += count; args.ReceivedData = new byte[count]; Array.Copy(buf, args.ReceivedData, count); this.DownloadProgressChanged(this, args); if (args.Canceled) { break; } } } stream.Close(); //解压 if (ResponseHeaders[HttpResponseHeader.ContentEncoding] != null) { MemoryStream msTemp = new MemoryStream(); count = 0; buf = new byte[100]; switch (ResponseHeaders[HttpResponseHeader.ContentEncoding].ToLower()) { case "gzip": GZipStream gzip = new GZipStream(ms, CompressionMode.Decompress); while ((count = gzip.Read(buf, 0, buf.Length)) > 0) { msTemp.Write(buf, 0, count); } return(msTemp.ToArray()); case "deflate": DeflateStream deflate = new DeflateStream(ms, CompressionMode.Decompress); while ((count = deflate.Read(buf, 0, buf.Length)) > 0) { msTemp.Write(buf, 0, count); } return(msTemp.ToArray()); default: break; } } return(ms.ToArray()); }