使用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。

祝您使用愉快!

相关推荐
beyond谚语几秒前
C#学习小笔记(完整版)—— Patience
c#
爱吃小胖橘18 分钟前
Unity网络开发--超文本传输协议Http(1)
开发语言·网络·网络协议·http·c#·游戏引擎
郝学胜-神的一滴26 分钟前
使用Linux的read和write系统函数操作文件
linux·服务器·开发语言·数据库·c++·程序人生·软件工程
小火柴12339 分钟前
利用R语言绘制直方图
开发语言·r语言
csbysj20201 小时前
React 表单与事件
开发语言
初圣魔门首席弟子1 小时前
c++ bug 函数定义和声明不一致导致出bug
开发语言·c++·bug
IT小农工1 小时前
Word 为每一页设置不同页边距(VBA 宏)
开发语言·c#·word
sali-tec2 小时前
C# 基于halcon的视觉工作流-章42-手动识别文本
开发语言·人工智能·算法·计算机视觉·c#·ocr
csbysj20202 小时前
中介者模式
开发语言