C#的http异步类

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace YRAutoArchive
{
    class HttpTool
    {
        private static readonly HttpClient client = new HttpClient();
        /*
         string url = "https://api.example.com";
        string responseBody = await HttpTool.SendGetRequest(url);
        Console.WriteLine("Response Body: " + responseBody);
         */
        public static async Task get(string url)
        {
            try
            {
                HttpResponseMessage response = await client.GetAsync(url);
                response.EnsureSuccessStatusCode(); // 确保响应成功

                string responseBody = await response.Content.ReadAsStringAsync();
                return responseBody;
            }
            catch (Exception ex)
            {
                Console.WriteLine("Failed to send GET request: " + ex.Message);
                return null;
            }
        }
        /*
         string url = "https://api.example.com/post";
         string requestBody = "{\"name\": \"John\", \"age\": 30}";
         string responseBody = await HttpTool.SendPostRequest(url, requestBody);
         Console.WriteLine("Response Body: " + responseBody);
         */
        public static async Task post(string url, string requestBody)
        {
            try
            {
                HttpContent content = new StringContent(requestBody);
                HttpResponseMessage response = await client.PostAsync(url, content);
                response.EnsureSuccessStatusCode(); // 确保响应成功

                string responseBody = await response.Content.ReadAsStringAsync();
                return responseBody;
            }
            catch (Exception ex)
            {
                Console.WriteLine("Failed to send POST request: " + ex.Message);
                return null;
            }
        }
        /*string url = "https://example.com/file.txt";
        string filePath = "path/to/save/file.txt";
        await HttpFileDownloader.DownloadFile(url, filePath);*/
        public static async Task download(string url, string filePath)
        {
            try
            {
                using (HttpClient client = new HttpClient())
                {
                    using (HttpResponseMessage response = await client.GetAsync(url))
                    {
                        response.EnsureSuccessStatusCode(); // 确保响应成功

                        using (Stream contentStream = await response.Content.ReadAsStreamAsync())
                        {
                            using (FileStream fileStream = File.Create(filePath))
                            {
                                await contentStream.CopyToAsync(fileStream);
                            }
                        }
                    }
                }
                Console.WriteLine("File downloaded successfully.");
            }
            catch (Exception ex)
            {
                Console.WriteLine("Failed to download file: " + ex.Message);
            }
        }
    }
}

你可能感兴趣的:(c#,http,linq)