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

祝您使用愉快!

相关推荐
小徐不徐说24 分钟前
序列化与反序列化:把 C++ 对象可靠地保存下来
开发语言·数据库·c++·php
ragnwang40 分钟前
Notes: Python pybind11 import C++ module
开发语言·c++·python
Wang's Blog1 小时前
Java 项目实战: 交友项目 - 搜索附近的功能概述和分析
java·开发语言·交友
骑着蜗牛撵大象3271 小时前
大模型 Agent 的沙箱护栏:gVisor 隔离、出口白名单与审计闭环
java·开发语言·jvm·安全·agent沙箱·gvisor·microvm
Dxy12393102162 小时前
JS DES 加密详解
开发语言·javascript·arcgis
充值内卷2 小时前
我给桌面加了个鱼缸:C++ 写后端,WebView2 里养一座 Three.js 小岛
开发语言·javascript·c++
云和数据.ChenGuang2 小时前
深入解析 Python 的 dir():从用法到 CPython 源码实现
开发语言·人工智能·python·langchain
nice_lcj5203 小时前
深入理解 Java 反射:从 Class 对象到方法调用(含性能实测 + 迷你 IOC 实战 + 面试考点)
java·开发语言·后端·面试
樱花树下的数学喵5 小时前
手写简易 STL string|C++ 字符串底层实现
开发语言·c++
jimy15 小时前
C++ 报错的 qualifiers 的意思
开发语言·c++