[C#/WEB API/.NET5] 대용량 데이터를 스트리밍 방식으로 전달받기 (PostgreSQL 연동)
■ 대용량 데이터를 스트리밍 방식으로 전달받는 방법을 보여준다. (PostgreSQL 연동) [TestClient 프로젝트] ▶ Program.cs
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 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
using System; using System.Collections.Generic; using System.IO; using System.Net; using System.Net.Http; using System.Net.Http.Headers; using System.Text; using Newtonsoft.Json; namespace TestClient { /// <summary> /// 프로그램 /// </summary> class Program { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 프로그램 시작하기 - Main() /// <summary> /// 프로그램 시작하기 /// </summary> static void Main() { string url = "https://localhost:44352/api/data/inquirylist"; DataSearchConditionModel searchCondition = new DataSearchConditionModel(); searchCondition.MENU_ID = "A007A6D5-35E6-46A2-A3BD-1B84843BB76C"; searchCondition.MEMBER_GRADE = "99"; searchCondition.LIMIT_AGE = "99"; searchCondition.WRITE_DATE1 = new DateTime(2021, 7 , 1 ); searchCondition.WRITE_DATE2 = new DateTime(2021, 7 , 25); searchCondition.CREATE_TIME1 = new DateTime(1 , 1 , 1 ); searchCondition.CREATE_TIME2 = new DateTime(9999, 12, 31); int i = 0; var result = GetPOSTData1<DataModel>(url, searchCondition); foreach(DataModel photo in result) { Console.WriteLine("{0} {1} {2}", i++, photo.ID, photo.SUBJECT); } } #endregion #region POST 스트림 구하기 - POSTGetStream(serverURL, contentType, encoding, argument, timeOut) /// <summary> /// POST 스트림 구하기 /// </summary> /// <param name="serverURL">서버 URL</param> /// <param name="contentType">컨텐츠 타입</param> /// <param name="encoding">인코딩</param> /// <param name="argument">인자 문자열</param> /// <param name="timeOut">타임 아웃</param> /// <returns>POST 스트림</returns> private static Stream POSTGetStream(string serverURL, string contentType, Encoding encoding, string argument, int? timeOut = null) { byte[] argumentArray = encoding.GetBytes(argument); HttpWebRequest request = WebRequest.Create(serverURL) as HttpWebRequest; request.ProtocolVersion = HttpVersion.Version11; request.AllowAutoRedirect = true; request.AllowWriteStreamBuffering = true; request.Method = WebRequestMethods.Http.Post; request.ContentType = contentType; request.ContentLength = argumentArray.Length; if(timeOut.HasValue) { request.Timeout = timeOut.Value; } using(Stream requestStream = request.GetRequestStream()) { requestStream.Write(argumentArray, 0, argumentArray.Length); } HttpWebResponse response = request.GetResponse() as HttpWebResponse; Stream responseStream = response.GetResponseStream(); return responseStream; } #endregion #region POST 데이터 구하기 1 - GetPOSTData1<TResultItem>(url, source) /// <summary> /// POST 데이터 구하기 1 /// </summary> /// <typeparam name="TResultItem">결과 항목 타입</typeparam> /// <param name="url">URL</param> /// <param name="source">소스 객체</param> /// <returns>결과 항목 타입 열거 가능형</returns> private static IEnumerable<TResultItem> GetPOSTData1<TResultItem>(string url, object source) { string sourceJSON = JsonConvert.SerializeObject(source); HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Post, url); requestMessage.Content = new StringContent(sourceJSON, Encoding.UTF8, "application/json"); requestMessage.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); HttpClient client = new HttpClient(); HttpResponseMessage responseMessage = client.SendAsync(requestMessage, HttpCompletionOption.ResponseHeadersRead).Result; responseMessage.EnsureSuccessStatusCode(); using(Stream stream = responseMessage.Content.ReadAsStreamAsync().Result) { using(StreamReader streamReader = new StreamReader(stream)) { JsonSerializer serializer = new JsonSerializer(); using(JsonTextReader jsonTextReader = new JsonTextReader(streamReader)) { while(jsonTextReader.Read()) { if(jsonTextReader.TokenType != JsonToken.StartArray && jsonTextReader.TokenType != JsonToken.EndArray) { yield return serializer.Deserialize<TResultItem>(jsonTextReader); } } } } } } #endregion #region POST 데이터 구하기 2 - GetPOSTData2<TResultItem>(url, source) /// <summary> /// POST 데이터 구하기 2 /// </summary> /// <typeparam name="TResultItem">결과 항목 타입</typeparam> /// <param name="url">URL</param> /// <param name="source">소스 객체</param> /// <returns>결과 항목 타입 열거 가능형</returns> private static IEnumerable<TResultItem> GetPOSTData2<TResultItem>(string url, object source) { string sourceJSON = JsonConvert.SerializeObject(source); using(Stream stream = POSTGetStream(url, "application/json; charset=utf-8", Encoding.UTF8, sourceJSON)) { using(StreamReader streamReader = new StreamReader(stream)) { using(JsonTextReader jsonTextReader = new JsonTextReader(streamReader)) { JsonSerializer serializer = new JsonSerializer(); while(jsonTextReader.Read()) { if(jsonTextReader.TokenType != JsonToken.StartArray && jsonTextReader.TokenType != JsonToken.EndArray) { yield return serializer.Deserialize<TResultItem>(jsonTextReader); } } } } } } #endregion } } |
[TestServer 프로젝트] ▶ launchSettings.json
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 |
{ "$schema" : "http://json.schemastore.org/launchsettings.json", "iisSettings" : { "windowsAuthentication" : false, "anonymousAuthentication" : true, "iisExpress" : { "applicationUrl" : "http://localhost:35043", "sslPort" : 44352 } }, "profiles" : { "IIS Express" : { "commandName" : "IISExpress", "launchBrowser" : true, "launchUrl" : "swagger", "environmentVariables" : { "ASPNETCORE_ENVIRONMENT" : "Development" } }, "TestServer" : { "commandName" : "Project", "dotnetRunMessages" : "true", "launchBrowser" : true, "launchUrl" : "swagger", "applicationUrl" : "https://localhost:5001;http://localhost:5000", "environmentVariables" : { "ASPNETCORE_ENVIRONMENT" : "Development" } } } } |
▶ appsettings.json