使用 C# 从 url 下载 .webp 图像 [英] Download .webp image from url with C#

查看:121
本文介绍了使用 C# 从 url 下载 .webp 图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从

下载图像

网址以 .jpg 结尾,但图片为 .WEBP 格式.

 using (WebClient wb = new WebClient()){wb.DownloadFile("http://aplweb.soriana.com/foto/fotolib//14/7503003936114/7503003936114-01-01-01.jpg", "image.jpg");}

我已经尝试过 .DownloadData()、asyng 方法、HttpClient、WebRequest 直接... 我总是遇到同样的错误.

有什么想法吗?

解决方案

您的代码很好,但这是特定于服务器的行为.添加一些请求标头可解决此问题.

这是一个使用 HttpClient

的例子

class 程序{私有静态只读 HttpClient 客户端 = new HttpClient(new HttpClientHandler() { AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate });静态异步任务 Main(string[] args){client.DefaultRequestHeaders.Accept.ParseAdd("text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");client.DefaultRequestHeaders.AcceptEncoding.ParseAdd("gzip, deflate");尝试{Console.WriteLine(正在下载...");byte[] data = await client.GetByteArrayAsync("http://aplweb.soriana.com/foto/fotolib//14/7503003936114/7503003936114-01-01-01.jpg");Console.WriteLine(正在保存...");File.WriteAllBytes("image.jpg", data);Console.WriteLine(OK.");}捕获(异常前){Console.WriteLine(ex.Message);}}}

控制台输出

正在下载...保存...好的.

下载的图片

I am trying to download an image from

http://aplweb.soriana.com/foto/fotolib/14/7503003936114/7503003936114-01-01-01.jpg

using WebClient.

When I browse the image in Chrome the image is there:

The url ends in .jpg but the image is in .WEBP format.

    using (WebClient wb = new WebClient())
    {                  
         wb.DownloadFile("http://aplweb.soriana.com/foto/fotolib//14/7503003936114/7503003936114-01-01-01.jpg", "image.jpg");
    }

I have tried .DownloadData(), asyng methods, HttpClient, WebRequest directly... and I am always getting the same error.

Any idea?

解决方案

Your code is fine but this is a server-specific behavior. Adding some request headers fixes the issue.

Here's an example using HttpClient

class Program
{
    private static readonly HttpClient client = new HttpClient(new HttpClientHandler() { AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate });

    static async Task Main(string[] args)
    {
        client.DefaultRequestHeaders.Accept.ParseAdd("text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
        client.DefaultRequestHeaders.AcceptEncoding.ParseAdd("gzip, deflate");
        try
        {
            Console.WriteLine("Downloading...");
            byte[] data = await client.GetByteArrayAsync("http://aplweb.soriana.com/foto/fotolib//14/7503003936114/7503003936114-01-01-01.jpg");
            Console.WriteLine("Saving...");
            File.WriteAllBytes("image.jpg", data);
            Console.WriteLine("OK.");
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }
}

Console output

Downloading...
Saving...
OK.

Downloaded image

这篇关于使用 C# 从 url 下载 .webp 图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆