【问题标题】:copy multiple remote files to another remote file via HttpWebRequest C#通过HttpWebRequest C#将多个远程文件复制到另一个远程文件
【发布时间】:2017-12-24 15:11:53
【问题描述】:

我有一个包含 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,因为我对此没有例外;

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

【问题讨论】:

  • 我猜你的请求正在超时尝试设置destRequest.Timeout ;destRequest.ReadWriteTimeout ;

标签: c# stream httpwebrequest httpwebresponse


【解决方案1】:

资源和目标请求存在混淆。 我已将 cmets 添加到更改的行中。

        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());
        }

【讨论】:

  • 我在 int blockSize = (int)sourceStream.Length; 上收到 NotSupportedException。例外是This stream does not support seek operations
  • 它可以在没有blockSize 的情况下调用sourceStream.CopyTo(destStream)。是正确的还是我有问题?
猜你喜欢
  • 1970-01-01
  • 2017-05-14
  • 2016-01-01
  • 2020-02-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-25
相关资源
最近更新 更多