Convert file network stream to memory stream according to Http download path

        /// <summary>
        /// Get the memory stream according to the file virtual path
        /// </ summary>
        /// <param name = "url"> http path </ param>
        /// <returns> </ returns>
        public static Stream GetUrlStream (string url)
        {
            try
            {
                HttpWebRequest myRequest = (HttpWebRequest) HttpWebRequest.Create (url); // Open network connection
                myRequest.AddRange (0);
                Stream readStream = myRequest.GetResponse (). GetResponseStream (); / / Request to the server to get the response data stream from the server
                MemoryStream memoryStream = new MemoryStream ();
                // Write the basic stream to the memory stream
                const int bufferLength = 1024;
                byte[] buffer = new byte[bufferLength];
                memoryStream.Position = 0;
                int contentSize = readStream.Read(buffer, 0, bufferLength);
                while (contentSize > 0)
                {

                    memoryStream.Write(buffer, 0, contentSize);
                    contentSize = readStream.Read(buffer, 0, bufferLength);
                    memoryStream.Position = memoryStream.Length;//重新定义流的追加位置
                }
                readStream.Close();
                return memoryStream;
            }
            catch (Exception ex)
            {

                throw ex;
            }

        }

Guess you like

Origin www.cnblogs.com/laukings/p/12745135.html