Beispiel #1
0
        private static async Task <OnlyUserDto> OnlyUserGetAsync(LoginResponseDto loginResponse)
        {
            OnlyUserDto onlyUserDto;

            using (HttpClient client = new HttpClient())
            {
                string endPoint = $"https://localhost:5001/api/OnlyUser";
                client.DefaultRequestHeaders.Authorization =
                    new AuthenticationHeaderValue("Bearer", loginResponse.Token);
                HttpResponseMessage response = null;
                response = await client.GetAsync(endPoint);

                String strResult = await response.Content.ReadAsStringAsync();

                Console.WriteLine($"狀態碼 : {response.StatusCode} ({(int)response.StatusCode})");
                Console.WriteLine($"取得結果 : {strResult}");
                APIResult apiResult;
                apiResult = JsonConvert.DeserializeObject <APIResult>(strResult,
                                                                      new JsonSerializerSettings {
                    MetadataPropertyHandling = MetadataPropertyHandling.Ignore
                });
                onlyUserDto = JsonConvert.DeserializeObject <OnlyUserDto>(apiResult.Payload.ToString(),
                                                                          new JsonSerializerSettings {
                    MetadataPropertyHandling = MetadataPropertyHandling.Ignore
                });
            }
            return(onlyUserDto);
        }
Beispiel #2
0
        private static async Task <(APIResult, LoginResponseDto)> LoginPostAsync(LoginRequestDto loginRequestDto)
        {
            APIResult        apiResult;
            LoginResponseDto loginResponseDto = null;

            using (HttpClient client = new HttpClient())
            {
                try
                {
                    #region 準備開啟連線
                    string endPoint = $"https://localhost:5001/api/login";
                    HttpResponseMessage response = null;

                    // Accept 用於宣告客戶端要求服務端回應的文件型態 (底下兩種方法皆可任選其一來使用)
                    //client.DefaultRequestHeaders.Accept.TryParseAdd("application/json");
                    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                    // Content-Type 用於宣告遞送給對方的文件型態
                    client.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json");

                    var JsonPayload = JsonConvert.SerializeObject(loginRequestDto);
                    // https://msdn.microsoft.com/zh-tw/library/system.net.http.stringcontent(v=vs.110).aspx
                    using (var fooContent = new StringContent(JsonPayload, Encoding.UTF8, "application/json"))
                    {
                        response = await client.PostAsync(endPoint, fooContent);
                    }
                    #endregion

                    #region 處理呼叫完成 Web API 之後的回報結果
                    if (response != null)
                    {
                        if (response.IsSuccessStatusCode == true)
                        {
                            // 取得呼叫完成 API 後的回報內容
                            String strResult = await response.Content.ReadAsStringAsync();

                            apiResult = JsonConvert.DeserializeObject <APIResult>(strResult,
                                                                                  new JsonSerializerSettings {
                                MetadataPropertyHandling = MetadataPropertyHandling.Ignore
                            });
                            loginResponseDto = JsonConvert.DeserializeObject <LoginResponseDto>(apiResult.Payload.ToString(),
                                                                                                new JsonSerializerSettings {
                                MetadataPropertyHandling = MetadataPropertyHandling.Ignore
                            });
                        }
                        else
                        {
                            apiResult = new APIResult
                            {
                                Status  = false,
                                Message = string.Format("Error Code:{0}, Error Message:{1}", response.StatusCode, response.RequestMessage),
                                Payload = null,
                            };
                        }
                    }
                    else
                    {
                        apiResult = new APIResult
                        {
                            Status  = false,
                            Message = $"應用程式呼叫 API ({endPoint}) 發生異常",
                            Payload = null,
                        };
                    }
                    #endregion
                }
                catch (Exception ex)
                {
                    apiResult = new APIResult
                    {
                        Status  = false,
                        Message = ex.Message,
                        Payload = ex,
                    };
                }
            }

            return(apiResult, loginResponseDto);
        }