【问题标题】:webclient.downloadfile not saving page completelywebclient.downloadfile 没有完全保存页面
【发布时间】:2013-02-23 15:09:12
【问题描述】:

我使用以下代码在我的 C# 应用程序中保存网页,但页面未完全保存:

WebClient webClient = new WebClient();
webClient.DownloadFile("http://jahanesanat.ir/currency.html", @"d://test/mytest66.html");

页面的某些部分没有保存在本地文件中 出了什么问题?

【问题讨论】:

  • 代码看起来不错。下载可能存在问题,或者您对“完整页面”的期望可能是错误的(未查看页面/未尝试代码)。稍微详细一点会很有用。
  • 此页面包含一些信息,当我使用 webclient.Downloadfile 时,其数据未完全保存,即页面中的某些部分在我保存的文件中不可见
  • “可见”是什么?您是否尝试在浏览器中打开本地保存的文件?检查文件的文本 - 它应该包含正确的 HTML 结束标记......如果没有 - 看看使用 Fiddler 跟踪请求是否显示任何有趣的问题。
  • 我猜,问题是“你怎么知道,页面的某些部分没有保存?”
  • 当我使用浏览器保存页面保存页面时,我的页面看起来与它的网页版本完全一样,但是当我使用 webclient 保存页面(通过代码)时,页面缺少一些信息(某些部分html 不包含在文件中)

标签: c# .net webclient webclient-download


【解决方案1】:

也许 d://test/ 被拒绝访问

使用 MSDN 中的这个示例

string remoteUri = "http://www.contoso.com/library/homepage/images/";
string fileName = "ms-banner.gif", myStringWebResource = null;
// Create a new WebClient instance.
WebClient myWebClient = new WebClient();
// Concatenate the domain with the Web resource filename.
myStringWebResource = remoteUri + fileName;
Console.WriteLine("Downloading File \"{0}\" from \"{1}\" .......\n\n", fileName, myStringWebResource);
// Download the Web resource and save it into the current filesystem folder.
myWebClient.DownloadFile(myStringWebResource,fileName);        
Console.WriteLine("Successfully Downloaded File \"{0}\" from \"{1}\"", fileName, myStringWebResource);
Console.WriteLine("\nDownloaded file saved in the following file system folder:\n\t" + Application.StartupPath)

【讨论】:

    猜你喜欢
    • 2013-04-23
    • 2011-12-14
    • 2014-12-08
    • 1970-01-01
    • 2016-09-08
    • 2012-01-07
    • 1970-01-01
    • 2019-06-27
    • 1970-01-01
    相关资源
    最近更新 更多