[.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:UsersclarkDesktopAAA.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("
    --" + boundaryString + "
    ");
    
                // Header
                string headerString = string.Format("Content-Disposition: form-data; name="{0}"; filename="{1}"
     Content-Type: application/octet-stream
    
    ", "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();
                }
            }                    
        }
    }
    
原文地址:https://www.cnblogs.com/clark159/p/6499288.html