【问题标题】:Image URL has the contentType "text/html"图片 URL 的 contentType 为“text/html”
【发布时间】:2012-09-20 21:47:59
【问题描述】:

我想实现一种将图像从网站下载到笔记本电脑的方法。

public static void DownloadRemoteImageFile(string uri, string fileName)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if ((response.StatusCode == HttpStatusCode.OK ||
            response.StatusCode == HttpStatusCode.Moved ||
            response.StatusCode == HttpStatusCode.Redirect) &&
            response.ContentType.StartsWith("image", StringComparison.OrdinalIgnoreCase))
        {
             //if the remote file was found, download it
            using (Stream inputStream = response.GetResponseStream())
            using (Stream outputStream = File.OpenWrite(fileName))
            {
                byte[] buffer = new byte[4096];
                int bytesRead;
                do
                {
                    bytesRead = inputStream.Read(buffer, 0, buffer.Length);
                    outputStream.Write(buffer, 0, bytesRead);
                } while (bytesRead != 0);
            }
        }
}

requestresponseContentType 不是“image/jpg”或“image/png”。它们总是“文本/html”。我想这就是为什么我将它们保存到本地后,它们的内容不正确,我无法查看它们。

任何人都可以在这里找到解决方案吗? 谢谢

【问题讨论】:

  • 您可能希望将代码放入代码块中并确保其格式正确。现在很难阅读。
  • 谢谢,我是新手,我正在学习正确地发布问题。很抱歉给您带来不便。
  • 你要传入什么网址?如果 url 没有托管 jpeg,您将获得它托管的任何内容。或者可能是该网站需要身份验证才能下载图像。
  • @feroze:这是示例网址。我认为它托管 jpg 文件 interfacelift.com/wallpaper/7yz4ma1/03085_steam_1366x768.jpg

标签: c# httpwebrequest httpwebresponse


【解决方案1】:

尝试将内容类型设置为特定的图像类型

Response.ContentType = "image/jpeg";

【讨论】:

  • Respoonse.ContentType 仅用于“获取”ContentType,我们不能使用它来设置 ContentType 以进行响应。我尝试在调用 GetResponse() 之前为 Request 设置 ContentType。但它也不起作用:(
  • 这是另一个 SO 帖子,希望对您有所帮助stackoverflow.com/questions/1271701/…
【解决方案2】:

您可以使用此代码 - 基于JpegBitmapDecoder class

JpegBitmapDecoder decoder = new JpegBitmapDecoder(YourImageStreamSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
//here you can adjust your YourImageStreamSource with outputStream value

BitmapSource bitmapSource = decoder.Frames[0];

Image myImage = new Image();
myImage.Source = bitmapSource;
myImage.Save("YourImage.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);

链接:http://msdn.microsoft.com/en-us/library/aa970689.aspx

【讨论】:

  • 我的问题不是解码图像。这是关于我的方法将响应保存为 html 类型和图像文件,所以我无法访问它。示例:我的网址是“abc.com/image.jpg”,我想将其保存到“C:\image.jpg”。网站上的原图是500KB,但是我用我的方法后,我的image.jpg是1KB,无法使用。
  • Image.Save("yourImage.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
【解决方案3】:

您希望从中获取图像的网站可能需要cookie。有时当我们使用浏览器访问该站点时,我们可能没有注意到它,但浏览器实际上可能会访问该站点一毫秒,然后快速重新加载,同时获取 cookie。但是在再次加载站点之前,我们的浏览器会在这一次将 cookie 传递给它,站点接受它并返回图像。

详细地说,这意味着您的方法将只执行浏览器实际执行的一半。 2 个 GET 请求方法的一半。第一个是获取 cookie,第二个是实际获取图像本身。

来自(可能有点相关)的​​信息:C# generate a cookie dynamically that site will accept?

【讨论】:

    【解决方案4】:

    您的代码没问题,但您尝试做的事情通常被网站所有者认为是不受欢迎的行为。大多数网站都希望您在网站上看到图像,但不要随意下载它们。您可以搜索与您的问题相反的内容,以了解您所反对的技术和保护措施。

    我强烈建议您在继续阅读之前阅读您尝试抓取的网站上的使用协议或任何类似文档。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-29
      • 2015-04-21
      • 1970-01-01
      • 2013-03-23
      • 2020-03-16
      • 1970-01-01
      • 1970-01-01
      • 2013-06-13
      相关资源
      最近更新 更多