■ WebClient 클래스를 사용해 외부 IP 주소를 구하는 방법을 보여준다.
▶ WebClient 클래스 : 외부 IP 주소 구하기 예제 (C#)
1 2 3 4 5 6 7 8 9 10 11 12 13 |
using System; using System.Net; string targetURL = "texture2d.iptime.org"; IPAddress[] ipAddressArray = Dns.GetHostAddresses(targetURL); foreach(IPAddress ipAddress in ipAddressArray) { Console.WriteLine(ipAddress.ToString()); } |
▶ WebClient 클래스 : 외부 IP 주소 구하기 (C#)
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 |
using System.Net; #region 외부 IP 주소 구하기 - GetExternalIPAddress() /// <summary> /// 외부 IP 주소 구하기 /// </summary> /// <returns>외부 IP 주소</returns> public string GetExternalIPAddress() { try { string targetURL = "http://www.whatismyip.com/automation/n09230945.asp"; WebClient webClient = new WebClient(); UTF8Encoding utf8Encoding = new UTF8Encoding(); string requestHtml = string.Empty; requestHtml = utf8Encoding.GetString(webClient.DownloadData(targetURL)); IPAddress ipAddress = null; ipAddress = IPAddress.Parse(requestHtml); return ipAddress.ToString(); } catch { return string.Empty; } } #endregion |
※ targetURL의 사이트에서 IP 정보를 제공해 준다.
※ 초기 테스트시 상기 타겟 URL의 사이트에서 IP 정보를 제공해 주었으나 2015/04/10일 기준 해당 기능이 폐쇄된 것으로 추정된다.