프로그램 자료/ASP.NET

도로명 주소 Open API 연동하기

motolies 2014. 12. 3. 09:50


도로명 주소 API 신청 : http://www.juso.go.kr/
 

asp.net 프로젝트로에 웹서비스 만들고 비동기로 검색할 때,
(code behind에 동기방식으로도 가능)

웹서비스를 하나 만들고 ~

위에서 도로명 주소 발급받은 키를 아래 ConfirmKey 라는 곳에 넣어주고 호출하면 끝!

데이터는 xml 형식으로 나오니까
code behind에서 사용하려면 xml 파싱해줘야 하고,

Ajax 사용시 xml 객체로 변경 후 파싱해주면 된다.


[WebMethod]

public string AddrSearch(Parameter[] parameters)

{

    try

    {

        ParameterList paramList = new ParameterList();

        paramList.Initialize(parameters);

 

        string Keyword = string.Empty;

        string PageNo = "1";

        string RowCount = "10";

 

        foreach (KeyValuePair<string, string> param in paramList.Keywords)

        {

            if (string.IsNullOrWhiteSpace(param.Value))

                continue;

 

            if (param.Key.Contains("Keyword"))

            {

                Keyword = param.Value;

            }

            if (param.Key.Contains("PageNo"))

            {

                PageNo = param.Value;

            }

            if (param.Key.Contains("RowCount"))

            {

                RowCount = param.Value;

            }

        }

 

        if (!string.IsNullOrWhiteSpace(Keyword))

        {

            string ConfirmKey = "발급받은 키";

            string url = string.Format("http://www.juso.go.kr/addrlink/addrLinkApi.do?keyword={0}&currentPage={1}&countPerPage={2}&confmKey={3}", Keyword, PageNo, RowCount, ConfirmKey);

 

            // Http Request

            HttpWebRequest wReq = (HttpWebRequest)WebRequest.Create(url);

            wReq.UserAgent = "Mozilla/4.0 (compatible; My Browser/1.0)";

            HttpWebResponse wRes = (HttpWebResponse)wReq.GetResponse();

 

            // xml 내용 추출

            Stream respPostStream = wRes.GetResponseStream();

        StreamReader readerPost = new StreamReader(respPostStream, Encoding.UTF8);

 

            return readerPost.ReadToEnd();

 

        }

        else

        {

            throw new Exception();

        }

 

    }

    catch (Exception x)

    {

        return "Error";

    }

}