private static async Task LoginAsync() { string url = "https://lobworkshop.azurewebsites.net/api/Login"; LoginRequestDTO loginRequestDTO = new LoginRequestDTO() { Account = "user50", Password = "******" }; var httpJsonPayload = JsonConvert.SerializeObject(loginRequestDTO); HttpClient client = new HttpClient(); HttpResponseMessage response = await client.PostAsync(url, new StringContent(httpJsonPayload, System.Text.Encoding.UTF8, "application/json")); if (response.IsSuccessStatusCode) { Console.WriteLine($"已經登入成功,將結果寫入到檔案中"); String strResult = await response.Content.ReadAsStringAsync(); APIResult apiResult = JsonConvert.DeserializeObject <APIResult>(strResult, new JsonSerializerSettings { MetadataPropertyHandling = MetadataPropertyHandling.Ignore }); if (apiResult.Status == true) { string itemJsonContent = apiResult.Payload.ToString(); LoginResponseDTO item = JsonConvert.DeserializeObject <LoginResponseDTO>(itemJsonContent, new JsonSerializerSettings { MetadataPropertyHandling = MetadataPropertyHandling.Ignore }); string content = JsonConvert.SerializeObject(item); await StorageUtility.WriteToDataFileAsync("", "MyDataFolder", "MyFilename.txt", content); } } }
static async Task Main(string[] args) { // 登入系統,取得 JTW Token await LoginAsync(); // 從檔案中取得 JWT 權杖 Token string fileContent = await StorageUtility.ReadFromDataFileAsync("", "MyDataFolder", "MyFilename.txt"); LoginResponseDTO loginResponseDTO = JsonConvert.DeserializeObject <LoginResponseDTO>(fileContent); #region CRUD => Retrive 取得該使用者的發票資料 List <InvoiceResponseDTO> invoiceResponseDTOs = await QueryInvoiceAsync(loginResponseDTO); PrintAllInvoice(invoiceResponseDTOs); #endregion #region CRUD => Create 新增發票資料 InvoiceResponseDTO invoiceResponseDTO = await CreateInvoiceAsync(loginResponseDTO); #endregion #region CRUD => Retrive 取得該使用者的發票資料 invoiceResponseDTOs = await QueryInvoiceAsync(loginResponseDTO); PrintAllInvoice(invoiceResponseDTOs); #endregion #region CRUD => Update 修改發票資料 invoiceResponseDTO = await UpdateInvoiceAsync(loginResponseDTO, invoiceResponseDTO); #endregion #region CRUD => Retrive 取得該使用者的發票資料 invoiceResponseDTOs = await QueryInvoiceAsync(loginResponseDTO); PrintAllInvoice(invoiceResponseDTOs); #endregion #region CRUD => Delete 刪除發票資料 foreach (var item in invoiceResponseDTOs) { await DeleteInvoiceAsync(loginResponseDTO, item.Id); } #endregion #region CRUD => Retrive 取得該使用者的發票資料 invoiceResponseDTOs = await QueryInvoiceAsync(loginResponseDTO); PrintAllInvoice(invoiceResponseDTOs); #endregion Console.WriteLine("Press any key for continuing..."); Console.ReadKey(); }