private static async Task ShowHttpClientRequestResponse() { string url = "https://lobworkshop.azurewebsites.net/api/Login"; LoginRequestDTO loginRequestDTO = new LoginRequestDTO() { Account = "user1", Password = "******" }; var httpJsonPayload = JsonConvert.SerializeObject(loginRequestDTO); HttpClient client = new HttpClient(new LoggingHandler(new HttpClientHandler())); 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) { await ShowHttpClientRequestResponse(); Console.WriteLine("讀取檔案內容並轉換成為物件的範例程式碼"); string fileContent = await StorageUtility.ReadFromDataFileAsync("", "MyDataFolder", "MyFilename.txt"); LoginResponseDTO loginResponseDTO = JsonConvert.DeserializeObject <LoginResponseDTO>(fileContent); Console.WriteLine($"{Environment.NewLine}JWT Token{Environment.NewLine}"); Console.WriteLine($"{loginResponseDTO.Token}"); Console.WriteLine("Press any key for continuing..."); Console.ReadKey(); }