본문 바로가기

Web_Application/C#

[다음 API] 주소 검색 샘플



다음 API 반환 구조 클래스

#region " [ 다음 주소검색 ] "

public class DaumAddressSearchResult
{
    public Channel channel { get; set; }
}

public class Channel
{
    public string totalCount { get; set; }
    public string link { get; set; }
    public string result { get; set; }
    public string generator { get; set; }
    public string pageCount { get; set; }
    public string lastBuildDate { get; set; }
    public Item[] item { get; set; }
    public string title { get; set; }
    public string description { get; set; }
}

public class Item
{
    public string mountain { get; set; }
    public string mainAddress { get; set; }
    public string point_wx { get; set; }
    public string point_wy { get; set; }
    public string isNewAddress { get; set; }
    public string buildingAddress { get; set; }
    public string title { get; set; }
    public string placeName { get; set; }
    public string zipcode { get; set; }
    public string newAddress { get; set; }
    public string localName_2 { get; set; }
    public string localName_3 { get; set; }
    public string localName_1 { get; set; }
    public float lat { get; set; }
    public float point_x { get; set; }
    public float lng { get; set; }
    public string zone_no { get; set; }
    public string subAddress { get; set; }
    public string id { get; set; }
    public float point_y { get; set; }
}

#endregion
결과 반환 클래스
#region " [ 결과 반환 클래스 ] "

public class ResultClass
{
    public int Code { get; set; }
    public string Message { get; set; }
    public string Data { get; set; }
    public string Etc { get; set; }
}

#endregion

실행 함수

//[HttpPost] //[ValidateAntiForgeryToken] public ResultClass DaumMapAddressSearch(string address) { string actualJson = string.Empty; string json = string.Empty; string serviceUrl = "https://apis.daum.net/local/geo/addr2coord?apikey={0}&output=json&q={1}"; string serviceKey = "{{API KEY}}"; string searchedAddress = string.Empty; int responseItemCount = 0; serviceUrl = string.Format(serviceUrl, serviceKey, address); try { _result.Code = -1; _result.Message = "EXCEPTION"; _result.Data = ""; WebRequest request = WebRequest.Create(serviceUrl); request.Credentials = CredentialCache.DefaultCredentials; WebResponse response = request.GetResponse(); Console.WriteLine(((HttpWebResponse)response).StatusDescription); Stream dataStream = response.GetResponseStream(); StreamReader reader = new StreamReader(dataStream); string responseFromServer = reader.ReadToEnd(); var responseResult = JsonConvert.DeserializeObject<DaumAddressSearchResult>(responseFromServer); if (responseResult != null) { responseItemCount = Convert.ToInt32(responseResult.channel.totalCount); if (responseItemCount > 0) { _result.Code = 0; _result.Message = "OK"; _result.Data = responseFromServer; } else { _result.Code = -2; _result.Message = "NO_DATA"; } } else { _result.Code = -2; _result.Message = "NO_DATA"; } } catch (Exception ex) { _result.Code = -1; _result.Message = "EXCEPTION"; _result.Data = ex.ToString(); } return _result; }