我正在尝试从下载图像
http://aplweb.soriana.com/foto/fotolib/14/7503003936114/7503003936114-01-01-01.jpg
使用WebClient。
网址以.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 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);
}
}
}
控制台输出
Downloading...
Saving...
OK.
下载图片
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句