C#/Winform

[C#] 초간단 REST API 호출 코드

kjun.kr 2022. 2. 22. 10:25
728x90
728x170
        #region GetData
        /// <summary>
        /// Get 수행 결과를 반환합니다.
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static T GetData<T>(string url)
        {
            using (HttpClient httpClient = new HttpClient())
            {
                var responseMessage = httpClient.GetAsync(url).Result;
                if (!responseMessage.IsSuccessStatusCode)
                {
                    return default(T);
                }

                string result = responseMessage.Content.ReadAsStringAsync().Result;

                return JsonConvert.DeserializeObject<T>(result);
            }
        }
        #endregion
728x90
그리드형