[.NET] 使用HttpClient操作HFS (HTTP File Server)

前言

本篇文章介绍如何使用HttpClient操作HFS (HTTP File Server),为自己留个纪录也希望能帮助到有需要的开发人员。关于HTTP File Server的介绍、安装、设定,可以参考下列参考资料:

功能

AddFile

  • Execute

    // Variables
    string url = @"http://localhost:8080/HFS/";
    string fileName = @"AAA.jpg";
    string filePath = @"C:\Users\clark\Desktop\AAA.jpg";
    
    // Execute
    this.AddFile(url, fileName, File.OpenRead(filePath));
    
  • Function

    public void AddFile(string url, string fileName, Stream fileStream)
    {
        #region Contracts
    
        if (string.IsNullOrEmpty(url) == true) throw new ArgumentException();
        if (string.IsNullOrEmpty(fileName) == true) throw new ArgumentException();
        if (fileStream == null) throw new ArgumentException();
    
        #endregion
    
        // FileStream
        using (fileStream)
        {
            // HttpClient
            using (var httpClient = new HttpClient())
            {
                // Boundary
                string boundaryString = "----------------------------" + DateTime.Now.Ticks.ToString("x");
                byte[] boundaryBytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundaryString + "\r\n");
    
                // Header
                string headerString = string.Format("Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\n Content-Type: application/octet-stream\r\n\r\n", "file", fileName);
                byte[] headerBytes = System.Text.Encoding.UTF8.GetBytes(headerString);
    
                // RequestContent               
                using (var memoryStream = new MemoryStream())
                using (var requestContent = new StreamContent(memoryStream))
                {
                    // Headers
                    requestContent.Headers.Add("Content-Type", "multipart/form-data; boundary=" + boundaryString);
    
                    // Content
                    memoryStream.Write(boundaryBytes, 0, boundaryBytes.Length);
                    memoryStream.Write(headerBytes, 0, headerBytes.Length);
                    fileStream.CopyTo(memoryStream);
                    memoryStream.Position = 0;
    
                    // Post
                    using (var response = httpClient.PostAsync(url, requestContent).Result)
                    {
                        // Response
                        if (response == null) throw new InvalidOperationException();
                        if (response.IsSuccessStatusCode == false) throw new InvalidOperationException();
                    }
                }
            }
        }
    }
    

RemoveFile

  • Execute

    // Variables
    string url = @"http://localhost:8080/HFS/";
    string fileName = @"AAA.jpg";
    
    // Execute
    this.RemoveFile(url, fileName);
    
  • Function

    public void RemoveFile(string url, string fileName)
    {
        #region Contracts
    
        if (string.IsNullOrEmpty(url) == true) throw new ArgumentException();
        if (string.IsNullOrEmpty(fileName) == true) throw new ArgumentException();
    
        #endregion
    
        // HttpClient
        using (var httpClient = new HttpClient())
        {
            // RequestContent      
            using (var requestContent = new FormUrlEncodedContent(new[]
            {
                // Content
                new KeyValuePair<string, string>("action", "delete"),
                new KeyValuePair<string, string>("selection", fileName)
            }))
            {
                // Post
                using (var response = httpClient.PostAsync(url, requestContent).Result)
                {
                    // Response
                    if (response == null) throw new InvalidOperationException();
                    if (response.IsSuccessStatusCode == false) throw new InvalidOperationException();
                }
            }                    
        }
    }
    
posted @   Clark159  阅读(740)  评论(0编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
历史上的今天:
2015-03-04 [ASP.NET MVC] 使用Bootsnipp样式
点击右上角即可分享
微信分享提示