如何处理下载流

发布于 2024-10-20 00:24:04 字数 1467 浏览 0 评论 0原文

我需要从安全链接下载 xml 文件,并将内容存储到数据库中。

我可以使用文本阅读器吗???或者我需要先将文件存储到本地文件系统中,然后从文件系统中读取内容并存储到数据库中?

HttpWebRequest downloadRequest = (HttpWebRequest)WebRequest.Create("https://line-to-xml-file.xml");
string Content;

                downloadRequest.Credentials = new NetworkCredential()
                                              {
                                                  UserName = this._userCredentials.UserName,
                                                  Password = this._userCredentials.Password
                                              };

                downloadRequest.PreAuthenticate = true;

                using (HttpWebResponse downloadHTTPResponse = (HttpWebResponse)downloadRequest.GetResponse())
                {
                    using (Stream downloadResponseStream = downloadHTTPResponse.GetResponseStream())
                    using (TextReader tReader = new StreamReader(downloadResponseStream))
                    {
                        Content = tReader.ReadToEnd();
                    }
                }
              return Content;

由于远程文件很大,高达 100MB,因此我在调试中看不到任何内容。 当我尝试保存它时。

using (TransactionScope trans = new TransactionScope()) <--- when comes to this line, exception throws...
{
// perform update, save the content into databse
// send a notification message to message bus, indicate content has been updated
}

它抱怨 MSDTC 事务超时/取消

I need to download xml file from a secure link, and store the content into database.

Can i use text reader??? or i need to store the file into my local file system first, then read the content from my file system and store into my database?

HttpWebRequest downloadRequest = (HttpWebRequest)WebRequest.Create("https://line-to-xml-file.xml");
string Content;

                downloadRequest.Credentials = new NetworkCredential()
                                              {
                                                  UserName = this._userCredentials.UserName,
                                                  Password = this._userCredentials.Password
                                              };

                downloadRequest.PreAuthenticate = true;

                using (HttpWebResponse downloadHTTPResponse = (HttpWebResponse)downloadRequest.GetResponse())
                {
                    using (Stream downloadResponseStream = downloadHTTPResponse.GetResponseStream())
                    using (TextReader tReader = new StreamReader(downloadResponseStream))
                    {
                        Content = tReader.ReadToEnd();
                    }
                }
              return Content;

Since the remote file is huge, up to 100MB, i can see nothing from debug.
and when i try to save it.

using (TransactionScope trans = new TransactionScope()) <--- when comes to this line, exception throws...
{
// perform update, save the content into databse
// send a notification message to message bus, indicate content has been updated
}

It complain MSDTC transaction timeout/cancelled

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

卸妝后依然美 2024-10-27 00:24:04

通过将其放入流中,您应该没问题...如果您对该特定流有任何问题,那么您可以使用 MemoryStream 而不是 FileStream 并以相同的方式使用它。但我毫不怀疑这就是你的情况。

我认为您还应该确保在保存流之前以及完全加载流后打开连接...您还可以使用您的 Command.TimeOut 属性,如果保存时间非常非常长,此处“值为 0表示没有限制”,但应该避免这种情况。

By having it into a stream you should be fine... if you have any problem with that specific stream then you could use a MemoryStream instead of a FileStream and use it the same way. But I hardly doubt this is your case.

I think you should make also sure to open the connection RIGHT BEFORE you are going to save the stream and when you have it completely loaded... You can also play with your Command.TimeOut property if it is taking really, really long to save, here "A value of 0 indicates no limit" but this should be avoided.
but

傻比既视感 2024-10-27 00:24:04

有一个 XmlReader,但如果 Xml 已经存在格式良好,您不需要解析它,因为您的数据库只是将其视为 blob,或者数据库引擎将解析它,您可以使用任何内容。

取决于您的数据库架构。

听起来数据库在插入时遇到问题,但需要更多信息。

There is an XmlReader, but if the Xml is already well-formed and you don't need to parse it because your database is just taking it as a blob, or the database engine is going to parse it, you could use whatever.

Depends on your database schema.

Sounds like the database is having trouble inserting it, but need more info.

孤寂小茶 2024-10-27 00:24:04

100MB 的文本对于塞进表格来说是很大的。你的陈述几乎肯定已经超时了。检查您的上下文和 SQL 命令对象(如果有)并增加超时值。

100MB of text is a lot to cram in a table. Your statement is almost certainly timing out. Check your context and your SQL command object (if you have one) and increase the timeout value.

慢慢从新开始 2024-10-27 00:24:04

您可能会设置更长的超时时间来解决超时问题,并且确保调用Complete函数

using (TransactionScope trans = new TransactionScope(
        TransactionScopeOption.Required, new TimeSpan(1, 4, 3))) // Sets time out to 1 hour 4 minutes and 3 seconds
{
  // perform update, save the content into databse
  // send a notification message to message bus, indicate content has been updated

  trans.Complete();
}

来读取文件,您可以使用WebClient。 WebClient 允许您监控进度。

    WebClient wc = new WebClient();
    wc.Credentials = new NetworkCredential()
    {
        UserName = this._userCredentials.UserName,
        Password = this._userCredentials.Password
    };

    wc.DownloadProgressChanged += new DownloadProgressChangedEventHandler(wc_DownloadProgressChanged);
    wc.DownloadFile("https://line-to-xml-file.xml", "C:\\local.xml");

如果需要,处理程序可以记录进度:

    void wc_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
    {
        // Log or show the current progress (e.ProgressPercentage or e.BytesReceived)
    }

如果您希望直接使用字符串而不需要再次读取文件,则可以使用 DownloadString 而不是 Download file。

    wc.DownloadString("https://line-to-xml-file.xml");

You will probably set a longer timeout duration to solve the timeout problem, and make sure you call the Complete function

using (TransactionScope trans = new TransactionScope(
        TransactionScopeOption.Required, new TimeSpan(1, 4, 3))) // Sets time out to 1 hour 4 minutes and 3 seconds
{
  // perform update, save the content into databse
  // send a notification message to message bus, indicate content has been updated

  trans.Complete();
}

as for reading the file, you could possible use WebClient. WebClient allows you to monitor the progress.

    WebClient wc = new WebClient();
    wc.Credentials = new NetworkCredential()
    {
        UserName = this._userCredentials.UserName,
        Password = this._userCredentials.Password
    };

    wc.DownloadProgressChanged += new DownloadProgressChangedEventHandler(wc_DownloadProgressChanged);
    wc.DownloadFile("https://line-to-xml-file.xml", "C:\\local.xml");

The handler could have log the progress if necessary:

    void wc_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
    {
        // Log or show the current progress (e.ProgressPercentage or e.BytesReceived)
    }

You could use DownloadString instead of Download file if you want the string straight without needing to read off the file again.

    wc.DownloadString("https://line-to-xml-file.xml");
极度宠爱 2024-10-27 00:24:04

How about using the WebClient and its download file method. Just save it locally, use it and delete upon completion of use.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文