c# - 通过HttpWebRequest C#将多个远程文件复制到另一个远程文件

标签 c# stream httpwebrequest httpwebresponse

我有一个包含 n 个文件的远程文件夹,我需要将内容复制到另一个远程文件中。我想这可以通过流来完成,这是我尝试过的:

            WebRequest destRequest = WebRequest.Create(destFile);
            destRequest.Method = "PUT";
            destRequest.Headers.Add("x-ms-blob-type", "BlockBlob"); //just an example with Azure blob, doesn't matter


            using (Stream destStream = destRequest.GetRequestStream())
            {
                string sourceName = "mysourcefolder";

                int blockSize = 8388608; //all the files have the same lenght, except one (sometimes)
                for (int i = 0; i < n; i++)
                {
                    string source = sourceName + i;
                    WebRequest sourceRequest = WebRequest.Create(source);
                    destRequest.Method = "GET";
                    HttpWebResponse destResp = (HttpWebResponse)destRequest.GetResponse();
                    using (Stream sourceStream = destResp.GetResponseStream())
                    {
                        sourceStream.CopyTo(destStream, blockSize);
                    }
                }

                Console.Write("ok");
            }

        }
        catch (Exception e)
        {
            Console.Write("nope !");
        }

我的代码中存在多个问题:

1) 我必须在我的 PUT 请求中指定长度。可能是 blockSize*n 因为我对此没有异常(exception);

2) 如果是这样,我仍然有异常Cannot close stream until all bytes are written。这是什么意思?

最佳答案

资源和目标请求存在混淆。 我已经在更改行中添加了注释。

        WebRequest destRequest = WebRequest.Create(destFile);
        destRequest.Method = "PUT";
        destRequest.Headers.Add("x-ms-blob-type", "BlockBlob"); //just an example with Azure blob, doesn't matter
        using (Stream destStream = destRequest.GetRequestStream())
        {
            string sourceName = "mysourcefolder";
            //int blockSize = 8388608; //all the files have the same lenght, except one (sometimes) //all the files have the same lenght, except one (sometimes)
            for (int i = 0; i < n; i++)
            {
                string source = sourceName + i;
                WebRequest sourceRequest = WebRequest.Create(source);
                destRequest.Method = "GET";

                //HttpWebResponse destResp = (HttpWebResponse)destRequest.GetResponse();
                //using (Stream sourceStream = destResp.GetResponseStream())

                // you need source response
                HttpWebResponse sourceResp = (HttpWebResponse)sourceRequest.GetResponse();
                using (Stream sourceStream = sourceResp.GetResponseStream())
                {
                    sourceStream.CopyTo(destStream);
                }
            }
            // The request is made here
            var destinationResponse = (HttpWebResponse) destRequest.GetResponse();
            //Console.Write("ok");
            Console.Write(destinationResponse.StatusCode.ToString());
        }

关于c# - 通过HttpWebRequest C#将多个远程文件复制到另一个远程文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45189001/

相关文章:

c# - MonoTouch 或 iOS 网络堆栈是否正在占用我的 HTTP DELETE 请求正文?

c# - 服务可以写入注册表 [HKLM]

scala - 使用返回 Future 的函数映射 Stream

c# - 返回由 Image.FromStream(Stream stream) 方法创建的图像

c# - 如何在不解析 C# 中的请求流的情况下根据内容长度拒绝和 HTTP Posts?

Java 无法从 URL 获取请求的文件

c# - 为什么同一个 4k 显示器上的 winform 显示不同

c# - 如何使用数据结构中所有字节的 8 位加法计算校验和

c# - 通过 ViewBag 将值从 Controller 传递到 View

c++ - iostream sentry 在到达末尾时不设置 failbit