■ WebClient 클래스 : 외부 IP 주소 구하기

------------------------------------------------------------------------------------------------------------------------

using System.Net;

using System.Text;

 

#region 외부 IP 주소 구하기 - GetExternalIPAddress()

 

/// <summary>

/// 외부 IP 주소 구하기

/// </summary>

/// <returns>외부 IP 주소</returns>

public string GetExternalIPAddress()

{

    string checkURL = "http://checkip.dyndns.org/";

 

    WebClient webClient = new WebClient();

 

    UTF8Encoding encoding = new UTF8Encoding();

 

    string html = encoding.GetString(webClient.DownloadData(checkURL));

 

    html = html.Substring(html.IndexOf("Current IP Address:"));

    html = html.Substring(0, html.IndexOf("</body>"));

    html = html.Split(':')[1].Trim();

 

    IPAddress ipAddress = null;

 

    ipAddress = IPAddress.Parse(html);

 

    return ipAddress.ToString();

}

 

#endregion

------------------------------------------------------------------------------------------------------------------------

Posted by 사용자 icodebroker

댓글을 달아 주세요