Browse Source

部分完善

yuanrf 6 months ago
parent
commit
6fda695e23
3 changed files with 611 additions and 469 deletions
  1. 526 469
      travelExport/Home.cs
  2. 1 0
      travelExport/travelExport.csproj
  3. 84 0
      travelExport/utility/HttpClientHelper.cs

File diff suppressed because it is too large
+ 526 - 469
travelExport/Home.cs


+ 1 - 0
travelExport/travelExport.csproj

@@ -553,6 +553,7 @@
     <Compile Include="Sys_Users_Encryption.cs">
       <DependentUpon>DB.tt</DependentUpon>
     </Compile>
+    <Compile Include="utility\HttpClientHelper.cs" />
     <Compile Include="utility\versions.cs" />
     <EmbeddedResource Include="Home.resx">
       <DependentUpon>Home.cs</DependentUpon>

+ 84 - 0
travelExport/utility/HttpClientHelper.cs

@@ -0,0 +1,84 @@
+using System;
+using System.Net.Http;
+using System.Text;
+using System.Threading.Tasks;
+using Newtonsoft.Json;
+
+public class HttpClientHelper
+{
+    private readonly HttpClient _httpClient;
+
+    public HttpClientHelper()
+    {
+        _httpClient = new HttpClient();
+        // 设置一些默认的请求头
+        _httpClient.DefaultRequestHeaders.Add("User-Agent", "C#-HttpClientHelper");
+    }
+
+    // 发送 GET 请求
+    public async Task<string> GetAsync(string url)
+    {
+        try
+        {
+            HttpResponseMessage response = await _httpClient.GetAsync(url);
+            response.EnsureSuccessStatusCode(); // 如果响应状态码不是 2xx,会抛出异常
+            return await response.Content.ReadAsStringAsync();
+        }
+        catch (Exception ex)
+        {
+            // 处理异常
+            return $"Error: {ex.Message}";
+        }
+    }
+
+    // 发送 POST 请求
+    public async Task<string> PostAsync(string url, object data)
+    {
+        try
+        {
+            var jsonContent = JsonConvert.SerializeObject(data);
+            var content = new StringContent(jsonContent, Encoding.UTF8, "application/json");
+
+            HttpResponseMessage response = await _httpClient.PostAsync(url, content);
+            response.EnsureSuccessStatusCode();
+            return await response.Content.ReadAsStringAsync();
+        }
+        catch (Exception ex)
+        {
+            return $"Error: {ex.Message}";
+        }
+    }
+
+    // 发送 PUT 请求
+    public async Task<string> PutAsync(string url, object data)
+    {
+        try
+        {
+            var jsonContent = JsonConvert.SerializeObject(data);
+            var content = new StringContent(jsonContent, Encoding.UTF8, "application/json");
+
+            HttpResponseMessage response = await _httpClient.PutAsync(url, content);
+            response.EnsureSuccessStatusCode();
+            return await response.Content.ReadAsStringAsync();
+        }
+        catch (Exception ex)
+        {
+            return $"Error: {ex.Message}";
+        }
+    }
+
+    // 发送 DELETE 请求
+    public async Task<string> DeleteAsync(string url)
+    {
+        try
+        {
+            HttpResponseMessage response = await _httpClient.DeleteAsync(url);
+            response.EnsureSuccessStatusCode();
+            return await response.Content.ReadAsStringAsync();
+        }
+        catch (Exception ex)
+        {
+            return $"Error: {ex.Message}";
+        }
+    }
+}