728x90
728x170

Public IP 주소를 가져오는 방법입니다.

http://checkip.dyndns.org 웹페이지로 접근하여 결과 값에서  IP 주소만 가져오도록 합니다.

private async void GetPublicIPAddress()
{
    HttpClient httpClient = new HttpClient();
    HttpResponseMessage response = await httpClient.GetAsync("http://checkip.dyndns.org");
    string content = await response.Content.ReadAsStringAsync();

    string ip = ParseIpAddress(content);

    Clipboard.SetText(ip);
    this.ipTtextBox.Text = ip;
}

static string ParseIpAddress(string content)
{
    int startIndex = content.IndexOf(':') + 1;
    int endIndex = content.IndexOf('<', startIndex);
    string ip = content.Substring(startIndex, endIndex - startIndex).Trim();
    return ip;
}
728x90
그리드형
Posted by kjun
,