【问题标题】:How to scrape the contents of an axd resource?如何抓取 axd 资源的内容?
【发布时间】:2009-10-16 22:24:40
【问题描述】:

基本上我有一个img 标签,其src 属性为/ChartImg.axd?i=chart_0_0.png&g=06469eea67ea452b977f8e73cad70691。我需要创建另一个 WebRequest 来获取此资源的内容还是有更简单的方法?

我正在抓取当前请求的输出。以下是我到目前为止所得到的......

基本上,我的附加资产在某些情况下将包含 .axd 资源的相对 Uri。我想将该内容包含在我正在构建的存档中。

    private void ProcessPrintRequest()
    {
        this.Response.Clear();
        this.Response.ContentType = "application/zip";
        this.Response.AddHeader("Content-Disposition", "attachment;filename=archive.zip");

        using (var stream = new ZipOutputStream(new ZeroByteStreamWrapper(this.Response.OutputStream)))
        {
            stream.SetLevel(9);

            var additionalAssets = new PathNormailzationDictionary();

            this.ExportDocument(stream, additionalAssets);
            this.ExportAdditionalAssets(stream, additionalAssets);
        }

        this.Response.End();
    }

    private void ExportAdditionalAssets(ZipOutputStream stream, PathNormailzationDictionary additionalAssets)
    {
        var buffer = new byte[32 * 1024];
        int read;

        // TODO: Request content of .axd resources
        foreach (var item in additionalAssets.Where(item => File.Exists(Server.MapPath(item.Key))))
        {
            var entry = new ZipEntry(item.Value);

            stream.PutNextEntry(entry);

            using (var fileStream = File.OpenRead(Server.MapPath(item.Key))) 
            {
                while ((read = fileStream.Read(buffer, 0, buffer.Length)) > 0)
                {
                    stream.Write(buffer, 0, read);
                }
            }
        }
    }

    private void ExportDocument(ZipOutputStream stream, PathNormailzationDictionary additionalAssets)
    {
        var entry = new ZipEntry("index.html");

        stream.PutNextEntry(entry);

        var document = this.GetNormalizedDocument(additionalAssets);

        var writer = new StreamWriter(stream);
        writer.Write(document);
        writer.Flush();
    }

    private string GetNormalizedDocument(PathNormailzationDictionary additionalAssets);

【问题讨论】:

    标签: c# asp.net screen-scraping


    【解决方案1】:

    是的,您必须创建另一个网络请求。任何给定的 HTML 页面都包含多个 http 请求;一个用于 html 页面,然后另一个用于每个外部 SRC。没有办法摆脱它。

    -奥辛

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-06-27
      • 2011-03-10
      • 2012-12-23
      • 1970-01-01
      • 2015-10-20
      • 1970-01-01
      • 2010-09-07
      相关资源
      最近更新 更多