๋ฐ˜์‘ํ˜•

๋„ค์ด๋ฒ„ ๋‰ด์Šค REST API / ์˜คํ”ˆ API

์ง€๋‚œ ํฌ์ŠคํŒ…์—์„œ Open API๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ C#์—์„œ ๊ณ ์†๋„๋กœ ๊ตํ†ต๋Ÿ‰ ์ •๋ณด๋ฅผ ์–ป๋Š” ๋ฐฉ๋ฒ•์„ ์†Œ๊ฐœํ–ˆ์—ˆ์Šต๋‹ˆ๋‹ค.

[C#] Open API ๊ณ ์†๋„๋กœ ๊ตํ†ต๋Ÿ‰ ์ •๋ณด ์–ป๊ธฐ (์˜ˆ์ œ ํฌํ•จ)

 

[C#] Open API ๊ณ ์†๋„๋กœ ๊ตํ†ต๋Ÿ‰ ์ •๋ณด ์–ป๊ธฐ (์˜ˆ์ œ ํฌํ•จ)

์†Œ๊ฐœ ์ €๋Š” ๋จธ์‹  ๋น„์ „ ๋ถ„์•ผ์— ๊ทผ๋ฌดํ•˜๊ณ  ์žˆ๊ณ , ์ฃผ๋กœ ๋น„์ „ ๊ด€๋ จ ํ”„๋กœ๊ทธ๋žจ์„ ๊ฐœ๋ฐœํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์‚ฌ์‹ค ๋งค์ผ ํ•˜๋˜ ๊ฒƒ๋งŒ ๊ฐœ๋ฐœํ•˜๋‹ค ๋ณด๋‹ˆ ์ง€๋ฃจํ•˜๊ธฐ๋„ ํ•˜๊ณ , ๊ฐœ๋ฐœ์˜ ๋ฐฉํ–ฅ์„ฑ์ด ๋„ˆ๋ฌด ํญ์ด ์ข์€ ๋Š๋‚Œ์ด ๋“ค์—ˆ

luckygg.tistory.com

๋„ค์ด๋ฒ„๋„ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์ด๋Ÿฌํ•œ Open API๋ฅผ ์ œ๊ณตํ•˜๋ฉฐ, ์‹ ์ฒญ ํ—ˆ๊ฐ€๋งŒ ๋œ๋‹ค๋ฉด ๋ˆ„๊ตฌ๋‚˜ ๋„ค์ด๋ฒ„ Open API๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ๋„ค์ด๋ฒ„ ๋‰ด์Šค Open API๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์›ํ•˜๋Š” ํ‚ค์›Œ๋“œ๊ฐ€ ํฌํ•จ๋œ ๋‰ด์Šค๋งŒ ์ˆ˜์ง‘ํ•˜๋Š” ๊ฒƒ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ์‚ฌ์‹ค ๋„ค์ด๋ฒ„ Open API๋Š” ๋‰ด์Šค๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ๋ธ”๋กœ๊ทธ, ๋ฐฑ๊ณผ์‚ฌ์ „, ์ง€์‹iN, ์˜คํƒ€ ๋ณ€ํ™˜ ๋“ฑ์˜ ์„œ๋น„์Šค API๋„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•๋งŒ ์•ˆ๋‹ค๋ฉด ์›ํ•˜๋Š” ๊ฒƒ์€ ๋‹ค ๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋„ค์ด๋ฒ„ ์˜คํ”ˆAPI ์„œ๋น„์Šค ์ข…๋ฅ˜

๋ณธ๋ฌธ์—์„œ๋Š” ๋„ค์ด๋ฒ„ Open API ์ด์šฉ ์‹ ์ฒญ ๋ฐฉ๋ฒ•๊ณผ C#์—์„œ ํŠน์ • ํ‚ค์›Œ๋“œ๊ฐ€ ํฌํ•จ๋œ ๋‰ด์Šค๋งŒ ์ˆ˜์ง‘ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์†Œ๊ฐœํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

๋„ค์ด๋ฒ„ ์˜คํ”ˆ API ์ด์šฉ ์‹ ์ฒญํ•˜๊ธฐ

์•„๋ž˜ ๋งํฌ์— ์ ‘์†ํ•˜์—ฌ ์˜คํ”ˆ API ์ด์šฉ ์‹ ์ฒญ ๋ฒ„ํŠผ์„ ํด๋ฆญํ•ฉ๋‹ˆ๋‹ค.

developers.naver.com/products/service-api/datalab/datalab.md

 

๋ฐ์ดํ„ฐ๋žฉ - SERVICE-API

๋ฐ์ดํ„ฐ๋žฉ ํ†ตํ•ฉ๊ฒ€์ƒ‰์–ด ํŠธ๋ Œ๋“œ ํ†ตํ•ฉ๊ฒ€์ƒ‰์–ด ํŠธ๋ Œ๋“œ๋Š” ๋„ค์ด๋ฒ„ ํ†ตํ•ฉ๊ฒ€์ƒ‰์—์„œ ๋ฐœ์ƒํ•˜๋Š” ๊ฒ€์ƒ‰์–ด๋ฅผ ์—ฐ๋ น๋ณ„, ์„ฑ๋ณ„, ๊ธฐ๊ธฐ๋ณ„(PC, ๋ชจ๋ฐ”์ผ)๋กœ ์„ธ๋ถ„ํ™”ํ•ด์„œ ์กฐํšŒํ•  ์ˆ˜ ์žˆ๋Š” API์ž…๋‹ˆ๋‹ค. ๋ถ„์„ํ•˜๊ณ  ์‹ถ์€ ์ฃผ์ œ๊ตฐ์„

developers.naver.com

์˜คํ”ˆ API ์ด์šฉ ์‹ ์ฒญ

์•„๋ž˜์™€ ๊ฐ™์ด ํ™˜์˜ ๋ฉ”์‹œ์ง€๊ฐ€ ํ‘œ์‹œ๋ฉ๋‹ˆ๋‹ค. ํ™•์ธ ๋ฒ„ํŠผ ํด๋ฆญ ํ›„ ์•ฝ๊ด€์— ๋™์˜ํ•ฉ๋‹ˆ๋‹ค.

์•ฝ๊ด€ ๋™์˜ ํ™”๋ฉด

ํœด๋Œ€ํฐ ๋ฐ ์ด๋ฉ”์ผ ์ •๋ณด ์ž…๋ ฅ ํ™”๋ฉด์ž…๋‹ˆ๋‹ค. ํšŒ์‚ฌ ์ด๋ฆ„์€ ์„ ํƒ์ด๋ฉฐ, ๊ฐœ์ธ์ด ๊ฐœ๋ฐœํ•˜๋Š” ๊ฒƒ์ด๋ผ๋ฉด ์ž…๋ ฅํ•˜์ง€ ์•Š์•„๋„ ๋ฉ๋‹ˆ๋‹ค.

๊ณ„์ • ์„ค์ • ํ™”๋ฉด

์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋“ฑ๋ก(API ์ด์šฉ์‹ ์ฒญ) ํ™”๋ฉด์ž…๋‹ˆ๋‹ค. ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ด๋ฆ„๊ณผ ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ์€ API์™€ ์„œ๋น„์Šค ํ™˜๊ฒฝ(Web, iOS, Android)์„ ์„ ํƒํ•ฉ๋‹ˆ๋‹ค.

์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋“ฑ๋ก ํ™”๋ฉด

๋ฌธ์ œ ์—†์ด ๋“ฑ๋ก๋๋‹ค๋ฉด, ์•„๋ž˜์™€ ๊ฐ™์ด ๋‚ด ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์— Client ID์™€ Secret ์ •๋ณด๊ฐ€ ํ‘œ์‹œ๋ฉ๋‹ˆ๋‹ค.

์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ํ™•์ธ ํ™”๋ฉด

๋„ค์ด๋ฒ„ API ๋‰ด์Šค ๊ฒ€์ƒ‰ ์š”์ฒญ๊ณผ ๊ฒฐ๊ณผ ๋ถ„์„ ๋ฐฉ๋ฒ•

1. ์š”์ฒญ ๋ฐฉ๋ฒ•

๋„ค์ด๋ฒ„ API ๋‰ด์Šค ๊ฒ€์ƒ‰ ๊ฐœ๋ฐœ ๊ฐ€์ด๋“œ์— ์•„๋ž˜์™€ ๊ฐ™์€ ์ •๋ณด๋ฅผ ์ œ๊ณตํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

๋‰ด์Šค ๊ฒ€์ƒ‰ API ๊ฐœ๋ฐœ ๊ฐ€์ด๋“œ - ๊ธฐ๋ณธ ์ •๋ณด ๋ฐ ์š”์ฒญ ๋ณ€์ˆ˜

API ๊ธฐ๋ณธ ์ •๋ณด๋Š” ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. 

๋ฉ”์„œ๋“œ ์ธ์ฆ ์š”์ฒญ URL ์ถœ๋ ฅ ํฌ๋งท
GET - https://openapi.naver.com/v1/search/news.xml XML
GET - https://openapi.naver.com/v1/search/news.json JSON

์ฆ‰, XML ํฌ๋งท์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›๊ณ  ์‹ถ๋‹ค๋ฉด https://openapi.naver.com/v1/search/news.xml URL์„ GET ๋ฉ”์„œ๋“œ๋กœ ํ˜ธ์ถœํ•˜๊ณ , JSON ํฌ๋งท์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›๊ณ  ์‹ถ๋‹ค๋ฉด https://openapi.naver.com/v1/search/news.json URL์„ GET ๋ฉ”์„œ๋“œ๋กœ ํ˜ธ์ถœํ•˜๋ผ๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ธ์ฆ์ด ํ•„์š” ์—†์Šต๋‹ˆ๋‹ค.

์š”์ฒญ ๋ณ€์ˆ˜๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

์š”์ฒญ ๋ณ€์ˆ˜ ํƒ€์ž… ํ•„์ˆ˜ ์—ฌ๋ถ€ ๊ธฐ๋ณธ๊ฐ’ ์„ค๋ช…
query string Y - ๊ฒ€์ƒ‰์„ ์›ํ•˜๋Š” ๋ฌธ์ž์—ด๋กœ์„œ UTF-8๋กœ ์ธ์ฝ”๋”ฉํ•œ๋‹ค.
display integer N 10(๊ธฐ๋ณธ๊ฐ’), 100(์ตœ๋Œ€) ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ ์ถœ๋ ฅ ๊ฑด์ˆ˜ ์ง€์ •.
start integer N 1(๊ธฐ๋ณธ๊ฐ’), 1000(์ตœ๋Œ€) ๊ฒ€์ƒ‰ ์‹œ์ž‘ ์œ„์น˜๋กœ ์ตœ๋Œ€ 1000๊นŒ์ง€ ๊ฐ€๋Šฅ.
sort string N sim, date(๊ธฐ๋ณธ๊ฐ’) ์ •๋ ฌ ์˜ต์…˜: sim (์œ ์‚ฌ๋„์ˆœ), date (๋‚ ์งœ์ˆœ).

๊ฒ€์ƒ‰์–ด(query)๋Š” ๋ฐ˜๋“œ์‹œ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๊ณ  ๋‚˜๋จธ์ง€ display, start, sort๋Š” ํ•„์ˆ˜๊ฐ€ ์•„๋‹ˆ๋ฏ€๋กœ ์„ ํƒ์ ์œผ๋กœ ์˜ต์…˜์„ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค.

์œ„ API ๊ธฐ๋ณธ ์ •๋ณด์™€ ์š”์ฒญ ๋ณ€์ˆ˜๋ฅผ ์–ด๋–ป๊ฒŒ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋Š”์ง€ ์˜ˆ์‹œ๋ฅผ ๋“ค๊ฒ ์Šต๋‹ˆ๋‹ค.

  • JSON ํฌ๋งท ์š”์ฒญ
  • ํ‚ค์›Œ๋“œ: ๊ฒฝ์ œ
  • ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ ์ถœ๋ ฅ ์ˆ˜: 40๊ฑด
  • ๊ฒ€์ƒ‰ ์‹œ์ž‘ ์œ„์น˜: 1
  • ์ •๋ ฌ ์˜ต์…˜: ์œ ์‚ฌ๋„์ˆœ
์š”์ฒญ URL: https://openapi.naver.com/v1/search/news.json?query=๊ฒฝ์ œ&display=40&start=1&sort=sim

2. ๊ฒฐ๊ณผ ๋ถ„์„ ๋ฐฉ๋ฒ•

์ถœ๋ ฅ ๊ฒฐ๊ณผ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

๋‰ด์Šค ๊ฒ€์ƒ‰ API ๊ฐœ๋ฐœ ๊ฐ€์ด๋“œ - ์ถœ๋ ฅ ๊ฒฐ๊ณผ

ํ•„๋“œ ํƒ€์ž… ์„ค๋ช…
rss - ๋””๋ฒ„๊ทธ๋ฅผ ์‰ฝ๊ฒŒ ํ•˜๊ณ  RSS ๋ฆฌ๋”๊ธฐ๋งŒ์œผ๋กœ ์ด์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•ด ๋งŒ๋“  RSS ํฌ๋งท์˜ ์ปจํ…Œ์ด๋„ˆ์ด๋ฉฐ ๊ทธ ์™ธ์˜ ํŠน๋ณ„ํ•œ ์˜๋ฏธ๋Š” ์—†๋‹ค.
channel - ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ๋ฅผ ํฌํ•จํ•˜๋Š” ์ปจํ…Œ์ด๋„ˆ์ด๋‹ค. ์ด ์•ˆ์— ์žˆ๋Š” title, link, description ๋“ฑ์˜ ํ•ญ๋ชฉ์€ ์ฐธ๊ณ ์šฉ์œผ๋กœ ๋ฌด์‹œํ•ด๋„ ๋ฌด๋ฐฉํ•˜๋‹ค.
lastBuildDate datetime ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ๋ฅผ ์ƒ์„ฑํ•œ ์‹œ๊ฐ„์ด๋‹ค.
total integer ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ ๋ฌธ์„œ์˜ ์ด ๊ฐœ์ˆ˜๋ฅผ ์˜๋ฏธํ•œ๋‹ค.
start integer ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ ๋ฌธ์„œ ์ค‘, ๋ฌธ์„œ์˜ ์‹œ์ž‘์ ์„ ์˜๋ฏธํ•œ๋‹ค.
display integer ๊ฒ€์ƒ‰๋œ ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ์˜ ๊ฐœ์ˆ˜์ด๋‹ค.
item/items - XML ํฌ๋ฉง์—์„œ๋Š” item ํƒœ๊ทธ๋กœ, JSON ํฌ๋ฉง์—์„œ๋Š” items ์†์„ฑ์œผ๋กœ ํ‘œํ˜„๋œ๋‹ค. ๊ฐœ๋ณ„ ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ์ด๋ฉฐ title, originallink, link, description, pubDate๋ฅผ ํฌํ•จํ•œ๋‹ค.
title string ๊ฐœ๋ณ„ ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ์ด๋ฉฐ, title, originallink, link, description, pubDate ๋ฅผ ํฌํ•จํ•œ๋‹ค.
originallink string ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ ๋ฌธ์„œ์˜ ์ œ๊ณต ์–ธ๋ก ์‚ฌ ํ•˜์ดํผํ…์ŠคํŠธ link๋ฅผ ๋‚˜ํƒ€๋‚ธ๋‹ค.
link string ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ ๋ฌธ์„œ์˜ ์ œ๊ณต ๋„ค์ด๋ฒ„ ํ•˜์ดํผํ…์ŠคํŠธ link๋ฅผ ๋‚˜ํƒ€๋‚ธ๋‹ค.
description string ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ ๋ฌธ์„œ์˜ ๋‚ด์šฉ์„ ์š”์•ฝํ•œ ํŒจ์‹œ์ง€ ์ •๋ณด์ด๋‹ค. ๋ฌธ์„œ ์ „์ฒด์˜ ๋‚ด์šฉ์€ link๋ฅผ ๋”ฐ๋ผ๊ฐ€๋ฉด ์ฝ์„ ์ˆ˜ ์žˆ๋‹ค. ํŒจ์‹œ์ง€์—์„œ ๊ฒ€์ƒ‰์–ด์™€ ์ผ์น˜ํ•˜๋Š” ๋ถ€๋ถ„์€ ํƒœ๊ทธ๋กœ ๊ฐ์‹ธ์ ธ ์žˆ๋‹ค.
pubData datetime ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ ๋ฌธ์„œ๊ฐ€ ๋„ค์ด๋ฒ„์— ์ œ๊ณต๋œ ์‹œ๊ฐ„์ด๋‹ค.

JSON ํฌ๋งท์œผ๋กœ ์š”์ฒญํ•œ ๊ฒฐ๊ณผ ์˜ˆ์‹œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

{
"lastBuildDate": "Wed, 17 Mar 2021 19:11:36 +0900",
"total": 219158,
"start": 1,
"display": 10,
"items": [
{
"title": "๋Œ€์ „ ์ž์น˜๊ตฌ ์ถ•์ œ ๊ฐœ์ตœ '์กฐ์‹ฌ์Šค๋Ÿฌ์šด ํƒ€์ง„'",
"originallink": "http://www.ggilbo.com/news/articleView.html?idxno=834720",
"link": "http://www.ggilbo.com/news/articleView.html?idxno=834720",
"description": "&lt;<b>์†๋ณด</b>&gt;=์ง€๋‚œํ•ด ์ฝ”๋กœ๋‚˜19๋กœ ์˜ฌ์Šคํ†ฑ๋œ ๋Œ€์ „ ์ง€์—ญ ์ถ•์ œ๋“ค์ด ์˜ฌํ•ด๋Š” ์กฐ๊ธˆ์”ฉ ๊ฐœ์ตœ๋  ๊ธฐ๋ฏธ๋ฅผ ๋ณด์ด๊ณ  ์žˆ๋‹ค. ๋Œ€์ „์—์„  ์ฝ”๋กœ๋‚˜19 ๊ฐ์—ผ์ด ์†Œ๊ฐ• ๊ตญ๋ฉด์„ ๋งž์ดํ•˜๊ธฐ๋„ ํ–ˆ์œผ๋ฉฐ ๋ฐฑ์‹  ์ ‘์ข…์œผ๋กœ ์ง‘๋‹จ๋ฉด์—ญ์ด ํ˜•์„ฑ๋  ๊ฒƒ์œผ๋กœ ๊ธฐ๋Œ€๋˜์ž ์ง€์—ญ <b>๊ฒฝ์ œ</b>... ",
"pubDate": "Wed, 17 Mar 2021 19:02:00 +0900"

},
{
"title": "[<b>์†๋ณด</b>] ๋†๋ฆผ์–ด์—…์ธ๋„ ๊ฐ€๊ตฌ๋‹น 100๋งŒ ์› ์žฌ๋‚œ์ง€์›๊ธˆ...์ถ”๊ฒฝ 1.6์กฐ ์ฆ์•ก",
"originallink": "https://hankookilbo.com/News/Read/A2021031717550004609?did=NA",
"link": "https://news.naver.com/main/read.nhn?mode=LSD&mid=sec&sid1=100&oid=469&aid=0000589056",
"description": "์ถ”๊ฒฝ ์ฆ์•ก ๊ฒฐ์ •์€ ์‹ ์ข… ์ฝ”๋กœ๋‚˜๋ฐ”์ด๋Ÿฌ์Šค ๊ฐ์—ผ์ฆ(์ฝ”๋กœ๋‚˜19)์œผ๋กœ ์ธํ•œ ๊ฒฝ๊ธฐ ์นจ์ฒด์™€ ๊ฝƒ ๋†๊ฐ€ ํƒ€๊ฒฉ ๋“ฑ <b>๊ฒฝ์ œ</b>์ ... ์ด๋“ค์€ “์ฝ”๋กœ๋‚˜19๋กœ <b>๊ฒฝ์ œ</b>์  ์ด์ค‘๊ณ ๋ฅผ ๊ฒฝํ—˜ํ•˜๊ณ  ์žˆ๋Š” ๋†๋ฆผ์–ด์—…์ธ์— ๋Œ€ํ•œ ์žฌ๋‚œ์ง€์›๊ธˆ ์ง€์› ๋ฐฉ์•ˆ์ด ๋น ์ง„ ๋ฐ... ",
"pubDate": "Wed, 17 Mar 2021 18:27:00 +0900"

},
 //...
]
}

C#์œผ๋กœ ๋„ค์ด๋ฒ„ ๋‰ด์Šค ๊ฒ€์ƒ‰ ํ”„๋กœ๊ทธ๋žจ ๋งŒ๋“ค๊ธฐ

์ด์ œ ๋ณธ๊ฒฉ์ ์œผ๋กœ C#์œผ๋กœ ํ”„๋กœ๊ทธ๋žจ์„ ๋งŒ๋“ค์–ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ExamNaverOpenAPI ์ด๋ฆ„์˜ Windows Forms์•ฑ ํ”„๋กœ์ ํŠธ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

Windows Forms ์•ฑ ํ”„๋กœ์ ํŠธ ์ƒ์„ฑํ•˜๊ธฐ

UI๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ๋ฐฐ์น˜ํ•ฉ๋‹ˆ๋‹ค. ํ‚ค์›Œ๋“œ, ์ถœ๋ ฅ ์ˆ˜, ์ •๋ ฌ ๋ฐฉ๋ฒ•์— ๊ธฐ๋ณธ ๊ฐ’์„ ์ž…๋ ฅํ•˜๋„๋ก ํ–ˆ์Šต๋‹ˆ๋‹ค. ๋‰ด์Šค ์กฐํšŒ ๊ฒฐ๊ณผ๋Š” ๋ฆฌ์ŠคํŠธ ๋ทฐ์— ํ‘œ์‹œ๋˜๋„๋ก ํ•  ์˜ˆ์ •์ž…๋‹ˆ๋‹ค.

๋‰ด์Šค ๊ฒ€์ƒ‰ ํ”„๋กœ๊ทธ๋žจ UI ์˜ˆ์‹œ

์ด์ œ ๋„ค์ด๋ฒ„ ์˜คํ”ˆ API์— ์–ด๋–ป๊ฒŒ ์š”์ฒญํ•˜๋Š”์ง€ ์ฝ”๋“œ๋ฅผ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

private string getResults()
{
	string keyword = textBoxKeyword.Text; //๊ฒ€์ƒ‰์–ด
	string display = trackBarDisplayCounts.Value.ToString(); //์ถœ๋ ฅ ํ‘œ์‹œ ์ˆ˜
	string sort = "sim"; //์ •๋ ฌ ๋ฐฉ๋ฒ•(์œ ์‚ฌ๋„)
	if (radioButtonDate.Checked == true)
		sort = "date"; //์ •๋ ฌ ๋ฐฉ๋ฒ•(๋‚ ์งœ)

	string query = string.Format("?query={0}&display={1}sort={2}", keyword, display, sort); //์ฟผ๋ฆฌ ๋งŒ๋“ค๊ธฐ

	WebRequest request = WebRequest.Create(_apiUrl + query); //์š”์ฒญ URL์— ์ฟผ๋ฆฌ ๋ถ™์—ฌ ๋ณด๋‚ด๊ธฐ
	request.Headers.Add("X-Naver-Client-Id", _clientId); //์ž์‹ ์˜ Application ID ์ž…๋ ฅ
	request.Headers.Add("X-Naver-Client-Secret", _clientSecret); //์ž์‹ ์˜ Application Secret ์ž…๋ ฅ

	string requestResult = "";
	using (var response = request.GetResponse())
	{
		using (Stream dataStream = response.GetResponseStream())
		{
			using (var reader = new StreamReader(dataStream))
			{
				requestResult = reader.ReadToEnd(); //๊ฒฐ๊ณผ ์ฝ์–ด๋‚ด๊ธฐ
			}
		}
	}
		
	return requestResult; //๊ฒฐ๊ณผ ๋ฐ˜ํ™˜
}

์•ž์„œ ์„ค๋ช…ํ•œ ๋Œ€๋กœ, ์˜ต์…˜ ์„ ํƒ์— ๋”ฐ๋ผ ์ฟผ๋ฆฌ๋ฅผ ๊ตฌ์„ฑํ•˜๊ณ  ํ•ด๋‹น URL์„ ์š”์ฒญํ•˜๋ฉด ํŠน์ • ํฌ๋งท์œผ๋กœ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. JSON์œผ๋กœ ์š”์ฒญํ•œ ๊ฒฐ๊ณผ๋Š” ์•„๋ž˜ ์ฝ”๋“œ์™€ ๊ฐ™์ด ํŒŒ์‹ฑ(Parsing)์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ์ฐธ๊ณ ๋กœ JSON ํŒŒ์‹ฑ์€ ๋ˆ„๊ฒŸ(Nuget)์—์„œ Newtonsoft.Json ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค.

private void buttonUpdate_Click(object sender, EventArgs e)
{
	try
	{
		string results = getResults(); //์š”์ฒญํ•œ ๊ฒฐ๊ณผ ๋ฐ˜ํ™˜
		//HTML ํƒœ๊ทธ ์ œ๊ฑฐ ๋ฐ ๋ณ€๊ฒฝ
		results = results.Replace("<b>", "");
		results = results.Replace("</b>", "");
		results = results.Replace("&lt;", "<");
		results = results.Replace("&gt;", ">");

		var parseJson = JObject.Parse(results);
		var countsOfDisplay = Convert.ToInt32(parseJson["display"]);
		var countsOfResults = Convert.ToInt32(parseJson["total"]);

		listViewResults.Items.Clear();
		for (int i=0; i< countsOfDisplay; i++)
		{
			ListViewItem item = new ListViewItem((i+1).ToString());
			
			var title = parseJson["items"][i]["title"].ToString();
			title = title.Replace("&quot;", "\""); //HTML ํƒœ๊ทธ ๋ณ€๊ฒฝ

			var description = parseJson["items"][i]["description"].ToString();
			description = description.Replace("&quot;", "\""); //HTML ํƒœ๊ทธ ๋ณ€๊ฒฝ

			var link = parseJson["items"][i]["link"].ToString();

			//๋‰ด์Šค ์ œ๋ชฉ, ๋ณธ๋ฌธ, ๋งํฌ๋ฅผ ๋ฆฌ์ŠคํŠธ ๋ทฐ์— ์ถ”๊ฐ€
			item.SubItems.Add(title);
			item.SubItems.Add(description);
			item.SubItems.Add(link);

			listViewResults.Items.Add(item);
		}
	}
	catch (Exception exc)
	{
		Debug.WriteLine(exc.Message);
	}
}

์š”์ฒญํ•œ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ›์•„๋ณด๋ฉด, ๋‰ด์Šค ์ œ๋ชฉ, ๋ณธ๋ฌธ์— ๋ถˆํ•„์š”ํ•œ HTML ํƒœ๊ทธ๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. <b>๋ผ๋“ ๊ฐ€ &quot; ๊ฐ™์€ ๊ฒƒ๋“ค์ธ๋ฐ์š”. ๊ฐ•์กฐ ํ‘œ์‹œ์ธ <b></b> ํƒœ๊ทธ๋Š” ํ•„์š” ์—†์œผ๋‹ˆ ์ œ๊ฑฐํ•˜๊ณ , &quot; ์™€ &lt;, &gt;๋Š” ํŠน์ˆ˜๋ฌธ์ž๋กœ ์น˜ํ™˜๋˜๋„๋ก ํ–ˆ์Šต๋‹ˆ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ๊ฐ ๋‰ด์Šค์˜ ์ œ๋ชฉ(title)๊ณผ ๋ณธ๋ฌธ(description), ๋งํฌ(link)๋ฅผ ํŒŒ์‹ฑํ•˜์—ฌ ๋ฆฌ์ŠคํŠธ ๋ทฐ์— ๋ณด์ด๋„๋ก ์ถ”๊ฐ€ํ•˜๋Š” ๊ฒƒ์ด ์ „๋ถ€์ž…๋‹ˆ๋‹ค. ๊ฒฐ๊ณผ๋ฅผ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

ํ”„๋กœ๊ทธ๋žจ ์‹คํ–‰ ๊ฒฐ๊ณผ

์ž์„ธํ•œ ์ฝ”๋“œ ์‚ฌ์šฉ์€ ์•„๋ž˜์˜ ์ƒ˜ํ”Œ ์ฝ”๋“œ๋ฅผ ์ฐธ๊ณ ํ•˜์„ธ์š”.

ExamNaverOpenAPI.zip
4.78MB

๋ฐ˜์‘ํ˜•

'Programming > .Net' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[.Net] C#์œผ๋กœ ๋Œ€์‹ ์ฆ๊ถŒ ์ฃผ์‹ ์ž๋™ ๋งค๋งค ํ”„๋กœ๊ทธ๋žจ ๋งŒ๋“ค๊ธฐ  (2) 2021.04.09
[.Net] C#์—์„œ ์นด์นด์˜คํ†ก ์˜คํ”ˆAPI/REST API๋กœ ๋‚˜์—๊ฒŒ ๋ฉ”์‹œ์ง€ ๋ณด๋‚ด๊ธฐ (์˜ˆ์ œ ํฌํ•จ)  (10) 2021.03.23
[.Net] ๊ณต๊ณต๋ฐ์ดํ„ฐํฌํ„ธ API ์‚ฌ์šฉํ•˜์—ฌ C#์—์„œ ์‹ค์‹œ๊ฐ„ ๋ฒ„์Šค ์œ„์น˜ ์กฐํšŒํ•˜๊ธฐ(์˜ˆ์ œ ํฌํ•จ)  (0) 2021.03.20
[.Net] C# ํ•œ๊ธ€๊ณผ ์ปดํ“จํ„ฐ HwpCtrl ์ ‘๊ทผ ํ—ˆ์šฉ ์˜ค๋ฅ˜ ํ•ด๊ฒฐํ•˜๊ธฐ  (1) 2021.03.16
[.Net] C# ํ”„๋กœ๊ทธ๋žจ ๋ฐฐํฌ ์‹œ ์†Œ์Šค ์ฝ”๋“œ ๋ณดํ˜ธ๋ฅผ ์œ„ํ•œ ๋‚œ๋…ํ™”, ConfuserEx ์‚ฌ์šฉ๋ฐฉ๋ฒ•  (5) 2021.03.14
[.Net] C# ํ•œ๊ธ€๊ณผ ์ปดํ“จํ„ฐ hwp ๋ฌธ์„œ ๋ถˆ๋Ÿฌ์˜ค๋Š” ํ”„๋กœ๊ทธ๋žจ ๋งŒ๋“ค๊ธฐ (์˜ˆ์ œ ํฌํ•จ)  (6) 2021.03.13
[.Net] C# WinForm์—์„œ Metro UI(Modern UI) Framework ์ ์šฉํ•˜๊ธฐ(์˜ˆ์ œ ํฌํ•จ)  (0) 2021.03.07