【问题标题】:Why when downloading html and save it as file on hard disk when editing the html file i see inside: [ Access Denied #2 ]?为什么在编辑我在里面看到的html文件时下载html并将其保存为硬盘上的文件:[访问被拒绝#2]?
【发布时间】:2014-02-27 09:07:19
【问题描述】:

代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Net;

namespace Battlefield_Servers_Ping
{
    public partial class Form1 : Form
    {
        private string appDirectory;
        private string htmlFilesDirectory;
        private string localFilename;
        private string mainurl;
        private string[] serversip;

        public Form1()
        {
            InitializeComponent();

            appDirectory = Path.GetDirectoryName(Application.LocalUserAppDataPath);
            htmlFilesDirectory = "HtmlFilesDirectory";
            htmlFilesDirectory = Path.Combine(appDirectory, htmlFilesDirectory);

            Directory.CreateDirectory(htmlFilesDirectory + "\\Tests");
            mainurl = "http://www.game-monitor.com/search.php?game=bfbc2&&num=";
            using (WebClient client = new WebClient())
            {
                for (int i = 10; i <= 100; i += 10)
                {
                    client.DownloadFile(mainurl + i, htmlFilesDirectory + "\\Tests\\" + i + ".html");
                }
            }

        }
    }
}

最后我在硬盘上有 10 个 html 文件,每个文件是 1kb,我看到里面:

[拒绝访问#2]

将它们在硬盘上的文件保存在我想要的目录中不是问题。网站有问题吗?

变量 htmlFilesDirectory 是:

C:\\Users\\bout0_000\\AppData\\Local\\Tests\\Tests\\HtmlFilesDirectory

网页来源为:

http://www.game-monitor.com/search.php?game=bfbc2&&num=10

如果我在此页面的浏览器 chrome 中的空白处进行保存,因为我可以保存 html 文件没有问题。并且稍后编辑它。 当我保存为文件名时:

Battlefield Bad Company 2 Servers   Server   Player Search   Game-Monitor.com    Server Search, Monitoring, Stats and more

但在我的程序中,我将它保存为 diffrenet。地址不一样。

【问题讨论】:

  • 您以什么权限运行您的应用程序?管理员?
  • 爸爸没有。以管理员身份尝试?
  • 现在作为管理员尝试过同样的事情。不工作。
  • 您将文件保存在哪里?您使用的是什么操作系统版本?
  • 我将用详细信息编辑我的问题。

标签: c# html winforms


【解决方案1】:

啊,我明白了。您无法从其他域保存这样的文件。

您需要做的是将 HTML 请求到流中并将其保存到文件中。

尝试使用 WebRequest: http://msdn.microsoft.com/en-us/library/system.net.webrequest(v=vs.110).aspx

【讨论】:

  • 它不工作。也许你可以试试我的代码看看。下载的 html 文件的内容是 [ Access Denied #2 ]。
【解决方案2】:

似乎该网站正在阻止来自浏览器的请求。要确认,您可以尝试使用DownloadString 方法,该方法将网页内容作为字符串返回:

var result = client.DownloadString(mainurl + i);

在此处设置断点,您将看到 "[ Access Denied #2 ]" 而不是浏览器中看到的 html 内容。

没有针对此限制的即时解决方法。您可能想尝试关注此帖子:

How can I emulate a web browser http request from code?

模拟从您的网络浏览器发送到网站的请求。

【讨论】:

    猜你喜欢
    • 2015-04-23
    • 1970-01-01
    • 1970-01-01
    • 2020-07-26
    • 2015-02-23
    • 1970-01-01
    • 2016-04-28
    • 1970-01-01
    • 2014-02-28
    相关资源
    最近更新 更多