使用C#和Flurl.Http库的下载器程序

根据您的要求,我为您编写了一个使用C#和Flurl.Http库的下载器程序,用于下载凤凰网的图片。以下是一个简单的示例代码:

csharp 复制代码
using System;
using Flurl.Http;

namespace DownloadImage
{
    class Program
    {
        static void Main(string[] args)
        {
            string url = "https://www.ifeng.com/";
            string filePath = "C:\\Download\\ifeng.com.png";

            DownloadImage(url, filePath);
        }

        static void DownloadImage(string url, string filePath)
        {
            using (var client = new FlurlClient())
            {
                var response = client.GetAsync(url).ReceiveJson();

                if (response["error"] == null)
                {
                    var imageUrl = response["data"]["imageUrl"].ToString();
                    DownloadImageToFile(imageUrl, filePath);
                }
                else
                {
                    Console.WriteLine("下载失败,错误信息:");
                    Console.WriteLine(response["message"]);
                }
            }
        }

        static void DownloadImageToFile(string imageUrl, string filePath)
        {
            using (var client = new FlurlClient())
            {
                var response = client.GetAsync(imageUrl).ReceiveString();
                var imageBytes = Convert.FromBase64String(response);
                using (var fileStream = System.IO.File.Create(filePath))
                {
                    fileStream.Write(imageBytes, 0, imageBytes.Length);
                }
            }
        }
    }
}

请将此代码保存为一个C#文件(例如:DownloadImage.cs),然后使用C#编译器编译并运行它。程序将尝试下载凤凰网的主页图片,并将其保存到指定的文件路径。

请注意,此代码仅适用于凤凰网的主页图片。如果您需要下载其他网站的图片,请确保提供正确的URL。

祝您使用愉快!

相关推荐
无限的鲜花5 小时前
反射(原创推荐)
java·开发语言
yongche_shi5 小时前
ragas官方文档中文版(五十)
开发语言·python·ai·ragas·如何评估和改进 rag 应用
一路向北he6 小时前
字节钢铁军团--“提供情境,而非控制”
java·开发语言·前端
AI行业学习7 小时前
Notepad++ 官方下载 + 完整安装 + 全套优化配置(2026最新)
开发语言·人工智能·python·前端框架·html·notepad++
大圣编程8 小时前
Python中continue语句的用法是什么?
开发语言·前端·python
upgrador8 小时前
基础知识:C++ STL构造函数的左闭右开惯例及其实现原理
开发语言·c++
yoothey9 小时前
报废审批流规则引擎设计——责任链模式完整实现
linux·开发语言·bash
geovindu9 小时前
python: Functional Options Pattern
开发语言·后端·python·设计模式·惯用法模式·函数式选项模式
wuyk5559 小时前
24. C 语言模块化:不是拆几个.c 文件那么简单
c语言·开发语言·stm32·单片机
北域码匠10 小时前
嵌入式限幅滤波:工业信号降噪利器
c#·传感器采集·数据预处理·嵌入式算法·限幅滤波·数字滤波·数据降噪