[.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();
                }
            }                    
        }
    }
    
期許自己
能以更簡潔的文字與程式碼,傳達出程式設計背後的精神。
真正做到「以形寫神」的境界。