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
그리드형
'C#' 카테고리의 다른 글
[C#] IHttpClientFactory 및 Polly 정책을 통해 HTTP 호출 재시도 구현하기 (0) | 2023.09.11 |
---|---|
[C#] Selenium 사용하여 웹사이트 열기 (0) | 2023.08.28 |
[C#] ArraySegment<T> Struct (0) | 2023.08.27 |
[C#] CsvHelper 사용하기 (CsvWriter CsvReader) (0) | 2023.08.07 |
[C#/Dapper] 42601: syntax error at or near “$1″ 에러 (0) | 2023.07.16 |