C# HttpListener 实现的HTTP Sever浏览器文件下载

 1. 前端页面请求

编写简单的test.html 文件,body体值配置a标签,其href 属性设置为文件下载请求的http接口要求的参数序列。

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>文件下载测试</title>
</head>

<body>
    <a href='http://localhost:8080/133419272086_1_1724083200_1724169600.mp4?MediaType=0&StreamType=0&StorageType=1&PlaybackMode=0&Multiple=1&DataSource=0&Speed=4&CTags=null' target='_blank'>文件下载</a>

</body>
</html>

浏览器打开如下:

发起请求,点击“文件下载”链接即可。

 2.后端响应处理

前端使用http接口发起请求,相当于客户端,因此后端需要开发服务端。使用C#开发后端响应处理程序。在C#中使用HttpServer,可以通过.Net Framework提供的HttpListener类来实现Http Server。具体代码实现如下:

1)创建服务端,开始监听

            HttpListener listener = new HttpListener();
            listener.Prefixes.Add("http://localhost:8080/"); // 设置HttpServer监听的地址和端口号

            listener.Start();

2)设置服务端响应文件内容类型,及文件

                HttpListenerResponse lsresponse = context.Response;
                //设置内容类型
                lsresponse.ContentType = "application/octet-stream";

                //准备文件路径
                string filePath = "./133419272086_1_1724083200_1724169600.mp4";

3)复制文件到响应流

                    //准备文件流
                    using (FileStream fs = File.OpenRead(filePath))
                    {
                        // 复制文件到响应流
                        await fs.CopyToAsync(lsresponse.OutputStream);
                        Console.WriteLine($"Sending: {filePath}");
                    }

3.测试

浏览器打开test.html,点击按钮“文件下载”

完整代码如下:

using System;
using System.Net;
using System.Text;
using System.IO;
using System.Threading.Tasks;
using System.Net.Http;

namespace HttpServerDemo
{
    class Program
    {
        static async Task Main(string[] args)
        {
Console.WriteLine("File Down Demo test.");
            HttpListener listener = new HttpListener();
            listener.Prefixes.Add("http://localhost:8080/"); // 设置HttpServer监听的地址和端口号

            listener.Start();

            Console.WriteLine("HttpServer started. Listening...");

            while (true)
            {
                HttpListenerContext context = await listener.GetContextAsync(); // 接收来自客户端的请求
                HttpListenerRequest request = context.Request;

                Console.WriteLine("Request received: " + request.ContentType + " " + request.HttpMethod + " " + request.Url);

                HttpListenerResponse lsresponse = context.Response;
                //设置内容类型
                lsresponse.ContentType = "application/octet-stream";

                //准备文件路径
                string filePath = "./133419272086_1_1724083200_1724169600.mp4";

                try
                {
                    //准备文件流
                    using (FileStream fs = File.OpenRead(filePath))
                    {
                        // 复制文件到响应流
                        await fs.CopyToAsync(lsresponse.OutputStream);
                        Console.WriteLine($"Sending: {filePath}");
                    }
                }
                catch(Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    lsresponse.StatusCode = 500;
                }
                finally
                {
                    lsresponse.Close();
                }
            }
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Shinobi_Jack

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值