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

祝您使用愉快!

相关推荐
前端世界8 小时前
当网络里混入“假网关”:用 Scapy 写一个 DHCP 欺骗检测器(附完整代码与讲解)
开发语言·网络·php
千里镜宵烛8 小时前
Lua-编译,执行和错误
开发语言·lua
赵谨言8 小时前
基于python二手车价值评估系统的设计与实现
大数据·开发语言·经验分享·python
张人玉9 小时前
WPF 控件速查 PDF 笔记(可直接落地版)(带图片)
大数据·microsoft·ui·c#·wpf
java1234_小锋9 小时前
PyTorch2 Python深度学习 - 初识PyTorch2,实现一个简单的线性神经网络
开发语言·python·深度学习·pytorch2
胡萝卜3.09 小时前
C++面向对象继承全面解析:不能被继承的类、多继承、菱形虚拟继承与设计模式实践
开发语言·c++·人工智能·stl·继承·菱形继承·组合vs继承
Violet_YSWY9 小时前
将axios、async、Promise联系在一起讲一下&讲一下.then 与其关系
开发语言·前端·javascript
luoganttcc10 小时前
用Python的trimesh库计算3DTiles体积的具体代码示例
开发语言·python·3d
我爱画页面10 小时前
vue3封装table组件及属性介绍
开发语言·javascript·ecmascript
逻极10 小时前
Next.js vs Vue.js:2025年全栈战场,谁主沉浮?
开发语言·javascript·vue.js·reactjs