|
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 |
public string TestForm(string ID, string IDX, string ProductNumber, string Parameter, int ContentsType) { string baseUrl = Config.ProductViewUrl; //Default string EMDViewUrl = Config.ProductEMDViewUrl; //EMD if (ContentsType == 2) // 2 : Product { baseUrl = EMDViewUrl; } var query = HttpUtility.ParseQueryString(string.Empty); query["ProductNumber"] = ProductNumber; query["Parameter"] = Parameter; string fullUrl = $"{baseUrl}?{query}"; string decoded = HttpUtility.UrlDecode(fullUrl); try { var response = httpClient.GetAsync(decoded).GetAwaiter().GetResult(); response.EnsureSuccessStatusCode(); return response.Content.ReadAsStringAsync().GetAwaiter().GetResult(); } catch (Exception ex) { // 로그 출력 또는 에러 핸들링 return $"Error: {ex.Message}"; } } |
✅ 변경 포인트
| 기존 | 변경 |
|---|---|
?ProductNumber=...&Parameter=... | HTTP Body(Form) 로 전송 |
HttpUtility.UrlDecode(fullUrl) | 불필요 → 제거 |
GetAsync | PostAsync |
✅ 장점
✔ 긴 XML/이름/주소 같은 값도 안전하게 전송
✔ QueryString 길이 제한 우회
✔ 특수문자/한글 자동 URL-encode
✔ Parameter 값이 중복되어도 ID/IDX/ProductNumber 는 인자로 확정됨
|
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 |
public string TestForm( string ID, string IDX, string ProductNumber, string Parameter, int ContentsType) { // 1) 기본 URL 선택 string endpoint = Config.ProductViewUrl; if (ContentsType == 2) // 2 = EMD endpoint = Config.ProductEMDViewUrl; try { // 2) Parameter 가 "&A=1&B=2" 형태일 수 있으므로 정리 if (!string.IsNullOrEmpty(Parameter) && Parameter.StartsWith("?")) Parameter = Parameter.Substring(1); var parsed = HttpUtility.ParseQueryString(Parameter ?? string.Empty); // 3) 최종 전송할 FormData 구성 var form = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase); // Parameter에 있는 모든 key/value 흡수 foreach (string key in parsed.AllKeys) { if (string.IsNullOrEmpty(key)) continue; form[key] = parsed[key] ?? string.Empty; } // 4) 직접 인자 값으로 덮어쓰기 form["ID"] = ID ?? string.Empty; form["IDX"] = IDX ?? string.Empty; form["ProductNumber"] = ProductNumber ?? string.Empty; form["ContentsType"] = ContentsType.ToString(); // 5) 필요 시 EMD URL 쪽에서 TktID 요구하면 추가 if (!form.ContainsKey("NiewID") && !string.IsNullOrEmpty(ID)) form["NiewID"] = ID; // 6) POST 전송 using (var content = new FormUrlEncodedContent(form)) { var response = httpClient.PostAsync(endpoint, content).GetAwaiter().GetResult(); response.EnsureSuccessStatusCode(); return response.Content.ReadAsStringAsync().GetAwaiter().GetResult(); } } catch (Exception ex) { return $"Error: {ex.Message}"; } } |
ParseQueryString 은 URL QueryString을 Key/Value 형태의 컬렉션(NameValueCollection)으로 파싱합니다.
파싱 과정에서 + → 공백, %xx → 원문 문자로 URL-Decoding 자동 처리됩니다.
✅ 2) form 이란?
- POST Body 로 보낼
x-www-form-urlencoded형식의 데이터를 담을Dictionary<string,string>입니다. StringComparer.OrdinalIgnoreCase옵션을 주었기 때문에 key 대소문자 구분 안 함
(예:"ID"와"id"를 동일 키로 처리)
|
1 2 3 4 5 6 7 8 |
foreach (string key in parsed.AllKeys) { if (string.IsNullOrEmpty(key)) continue; form[key] = parsed[key] ?? string.Empty; } |
✅ 3) 파싱된 Parameter 값들을 Dictionary(form) 에 모두 복사
parsed.AllKeys→ Parameter에서 추출된 모든 Key 목록- Key가 비어있지 않다면
Dictionary에 추가
(Value 가 null이면""로 대체)
✅ 결과:Parameter 로 들어온 모든 값이 form[] 안에 들어가게 됩니다.
