■ HttpClient 클래스를 사용해 POST 요청 열거 가능형을 구하는 방법을 보여준다.
▶ 예제 코드 (C#)
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 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
using System; using System.Collections.Generic; using System.Net; using System.Net.Http; using System.Net.Http.Headers; using System.Threading.Tasks; #region POST 요청 열거 가능형 구하기 (비동기) - GetPOSTRequestEnumerableAsync<T>(url, tokenSchema, token, input) /// <summary> /// POST 요청 열거 가능형 구하기 (비동기) /// </summary> /// <typeparam name="TItem">항목 타입</typeparam> /// <param name="url">URL</param> /// <param name="tokenSchema">토큰 스키마</param> /// <param name="token">토큰</param> /// <param name="input">입력 객체</param> /// <returns>POST 요청 열거 가능형 태스크</returns> public async Task<IEnumerable<TItem>> GetPOSTRequestEnumerableAsync<TItem>(string url, string tokenSchema, string token, object input) { IEnumerable<TItem> resultEnumerable = await GetPOSTRequestObjectAsync<IEnumerable<TItem>>(url, tokenSchema, token, input); return resultEnumerable; } #endregion #region POST 요청 열거 가능형 구하기 (비동기) - GetPOSTRequestEnumerableAsync<T>(url, token, input) /// <summary> /// POST 요청 열거 가능형 구하기 (비동기) /// </summary> /// <typeparam name="TItem">항목 타입</typeparam> /// <param name="url">URL</param> /// <param name="token">토큰</param> /// <param name="input">입력 객체</param> /// <returns>POST 요청 열거 가능형 태스크</returns> public async Task<IEnumerable<TItem>> GetPOSTRequestEnumerableAsync<TItem>(string url, string token, object input) { IEnumerable<TItem> resultEnumerable = await GetPOSTRequestObjectAsync<IEnumerable<TItem>>(url, "Bearer", token, input); return resultEnumerable; } #endregion #region POST 요청 객체 구하기 (비동기) - GetPOSTRequestObjectAsync<TObject>(url, tokenSchema, token, input) /// <summary> /// POST 요청 객체 구하기 (비동기) /// </summary> /// <typeparam name="TObject">객체 타입</typeparam> /// <param name="url">URL</param> /// <param name="tokenSchema">토큰 스키마</param> /// <param name="token">토큰</param> /// <param name="input">입력 객체</param> /// <returns>POST 요청 객체 태스크</returns> private async Task<TObject> GetPOSTRequestObjectAsync<TObject>(string url, string tokenSchema, string token, object input) { using HttpClient client = GetHTTPClient(false, url, tokenSchema, token); HttpResponseMessage response = await client.PostAsJsonAsync(url, input); if(response.IsSuccessStatusCode) { TObject result = await response.Content.ReadAsAsync<TObject>(); return result; } else { string finalURL = response.RequestMessage.RequestUri.AbsoluteUri; if(finalURL != url) { response = await client.PostAsJsonAsync(finalURL, input); if(response.IsSuccessStatusCode) { TObject result = await response.Content.ReadAsAsync<TObject>(); return result; } else { return default; } } else { return default; } } } #endregion #region POST 요청 객체 구하기 (비동기) - GetPOSTRequestObjectAsync<TObject>(url, token, input) /// <summary> /// POST 요청 객체 구하기 (비동기) /// </summary> /// <typeparam name="TObject">객체 타입</typeparam> /// <param name="url">URL</param> /// <param name="token">토큰</param> /// <param name="input">입력 객체</param> /// <returns>POST 요청 객체 태스크</returns> private async Task<TObject> GetPOSTRequestObjectAsync<TObject>(string url, string token, object input) { TObject result = await GetPOSTRequestObjectAsync<TObject>(url, "Bearer", token, input); return result; } #endregion #region HTTP 클라이언트 구하기 - GetHTTPClient(useCookie, baseAddress, tokenShema, token) /// <summary> /// HTTP 클라이언트 구하기 /// </summary> /// <param name="useCookie">쿠키 사용 여부</param> /// <param name="baseAddress">기본 주소</param> /// <param name="tokenShema">토큰 스키마</param> /// <param name="token">토큰</param> /// <returns>HTTP 클라이언트</returns> private HttpClient GetHTTPClient(bool useCookie, string baseAddress, string tokenShema, string token) { HttpClient client; if(useCookie) { HttpClientHandler httpClientHandler = new HttpClientHandler(); httpClientHandler.UseCookies = true; httpClientHandler.CookieContainer = new CookieContainer(); client = new HttpClient(httpClientHandler); } else { client = new HttpClient(); } client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); if(!string.IsNullOrWhiteSpace(baseAddress)) { client.BaseAddress = new Uri(baseAddress); } if(!string.IsNullOrWhiteSpace(tokenShema)) { client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(tokenShema, token); } return client; } #endregion |