■ RestSharp을 사용해 REST를 호출하는 방법을 보여준다.
[TestClient 프로젝트]
▶ Program.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
using System; using Newtonsoft.Json; using RestSharp; using TestLibrary.Models; namespace TestClient { /// <summary> /// 프로그램 /// </summary> class Program { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region 프로그램 시작하기 - Main() /// <summary> /// 프로그램 시작하기 /// </summary> static void Main() { UserModel user = new UserModel { UserName = "admin", Password = "1234" }; string token = Login("https://localhost:44364/api/authentication/login", user); Console.WriteLine($"JWT 토큰 : {token}"); Console.WriteLine("--------------------------------------------------"); TestModel test = new TestModel { Value = "테스트" }; string result = Test("https://localhost:44364/api/authentication/test", token, test); Console.WriteLine($"결과 : {result}"); Console.WriteLine("--------------------------------------------------"); Console.WriteLine("아무 키나 눌러 주시기 바랍니다."); Console.ReadKey(true); } #endregion #region 로그인하기 - Login(url, user) /// <summary> /// 로그인하기 /// </summary> /// <param name="url">URL</param> /// <param name="user">사용자</param> /// <returns>JWT 토큰</returns> private static string Login(string url, UserModel user) { RestClient restClient = new RestClient(url); restClient.Timeout = -1; RestRequest restRequest = new RestRequest(Method.POST); restRequest.AddHeader("Content-Type", "application/json"); string json = JsonConvert.SerializeObject(user); restRequest.AddParameter("application/json", json, ParameterType.RequestBody); IRestResponse restResponse = restClient.Execute(restRequest); string token = restResponse.Content.Replace("\"", ""); return token; } #endregion #region 테스트하기 - Test(url, token, test) /// <summary> /// 테스트하기 /// </summary> /// <param name="url">URL</param> /// <param name="token">JWT 토큰</param> /// <param name="test">테스트</param> /// <returns>테스트 결과</returns> private static string Test(string url, string token, TestModel test) { RestClient restClient = new RestClient(url); restClient.Timeout = -1; RestRequest restRequest2 = new RestRequest(Method.POST); restRequest2.AddHeader("Content-Type" , "application/json"); restRequest2.AddHeader("Authorization", $"Bearer {token}" ); string json = JsonConvert.SerializeObject(test); restRequest2.AddParameter("application/json", json, ParameterType.RequestBody); IRestResponse restResponse = restClient.Execute(restRequest2); string result = restResponse.Content.Replace("\"", ""); return result; } #endregion } } |