修改接口
This commit is contained in:
		
							
								
								
									
										60
									
								
								TestApp/Program.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										60
									
								
								TestApp/Program.cs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,60 @@
 | 
			
		||||
using System;
 | 
			
		||||
using System.Net.Http;
 | 
			
		||||
using System.Text;
 | 
			
		||||
using System.Text.Json;
 | 
			
		||||
using System.Threading.Tasks;
 | 
			
		||||
 | 
			
		||||
namespace TestApp
 | 
			
		||||
{
 | 
			
		||||
    class Program
 | 
			
		||||
    {
 | 
			
		||||
        static async Task Main(string[] args)
 | 
			
		||||
        {
 | 
			
		||||
            var httpClient = new HttpClient();
 | 
			
		||||
            var baseUrl = "http://localhost:5003/api/v1";
 | 
			
		||||
            
 | 
			
		||||
            // 1. 先尝试登录现有用户
 | 
			
		||||
            Console.WriteLine("尝试登录现有用户...");
 | 
			
		||||
            var loginData = new
 | 
			
		||||
            {
 | 
			
		||||
                usernameOrEmail = "newuser@example.com",
 | 
			
		||||
                password = "newpass123"
 | 
			
		||||
            };
 | 
			
		||||
            
 | 
			
		||||
            var loginContent = new StringContent(JsonSerializer.Serialize(loginData), Encoding.UTF8, "application/json");
 | 
			
		||||
            var loginResponse = await httpClient.PostAsync($"{baseUrl}/auth/login", loginContent);
 | 
			
		||||
            
 | 
			
		||||
            if (loginResponse.IsSuccessStatusCode)
 | 
			
		||||
            {
 | 
			
		||||
                var loginResponseContent = await loginResponse.Content.ReadAsStringAsync();
 | 
			
		||||
                Console.WriteLine($"登录成功: {loginResponseContent}");
 | 
			
		||||
                
 | 
			
		||||
                // 解析响应获取token
 | 
			
		||||
                using (JsonDocument doc = JsonDocument.Parse(loginResponseContent))
 | 
			
		||||
                {
 | 
			
		||||
                    var root = doc.RootElement;
 | 
			
		||||
                    if (root.TryGetProperty("data", out var dataElement) && 
 | 
			
		||||
                        dataElement.TryGetProperty("token", out var tokenElement))
 | 
			
		||||
                    {
 | 
			
		||||
                        var token = tokenElement.GetString();
 | 
			
		||||
                        
 | 
			
		||||
                        // 2. 使用获取的token调用Mails接口
 | 
			
		||||
                        Console.WriteLine("\n使用token调用Mails接口...");
 | 
			
		||||
                        httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);
 | 
			
		||||
                        var mailsResponse = await httpClient.GetAsync($"{baseUrl}/Mails?PageIndex=1&PageSize=20");
 | 
			
		||||
                        
 | 
			
		||||
                        var mailsResponseContent = await mailsResponse.Content.ReadAsStringAsync();
 | 
			
		||||
                        Console.WriteLine($"Mails接口响应状态: {mailsResponse.StatusCode}");
 | 
			
		||||
                        Console.WriteLine($"Mails接口响应内容: {mailsResponseContent}");
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
            else
 | 
			
		||||
            {
 | 
			
		||||
                var loginResponseContent = await loginResponse.Content.ReadAsStringAsync();
 | 
			
		||||
                Console.WriteLine($"登录失败: {loginResponse.StatusCode}");
 | 
			
		||||
                Console.WriteLine($"响应内容: {loginResponseContent}");
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user