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

祝您使用愉快!

相关推荐
mudtools15 小时前
搭建一套.net下能落地的飞书考勤系统
后端·c#·.net
玩泥巴的1 天前
搭建一套.net下能落地的飞书考勤系统
c#·.net·二次开发·飞书
唐宋元明清21881 天前
.NET 本地Db数据库-技术方案选型
windows·c#
郑州光合科技余经理1 天前
代码展示:PHP搭建海外版外卖系统源码解析
java·开发语言·前端·后端·系统架构·uni-app·php
lindexi1 天前
dotnet DirectX 通过可等待交换链降低输入渲染延迟
c#·directx·d2d·direct2d·vortice
feifeigo1231 天前
matlab画图工具
开发语言·matlab
dustcell.1 天前
haproxy七层代理
java·开发语言·前端
norlan_jame1 天前
C-PHY与D-PHY差异
c语言·开发语言
多恩Stone1 天前
【C++入门扫盲1】C++ 与 Python:类型、编译器/解释器与 CPU 的关系
开发语言·c++·人工智能·python·算法·3d·aigc
QQ4022054961 天前
Python+django+vue3预制菜半成品配菜平台
开发语言·python·django