C# 取得 機器 廣域/真實/實際 IP (Global IP/public ip/external ip address)
C# 取得 機器 廣域/真實/實際 IP (Global IP/public ip/external ip address)
資料來源: https://www.c-sharpcorner.com/blogs/how-to-get-public-ip-address-using-c-sharp1
https://www.c-sharpcorner.com/blogs/how-to-get-public-ip-address-using-c-sharp1
https://codingvision.net/c-how-to-get-external-ip-address
code
public static string GetPublicIPAddress(int mode=0)
{
//https://www.c-sharpcorner.com/blogs/how-to-get-public-ip-address-using-c-sharp1
//https://www.codegrepper.com/code-examples/csharp/c%23+get+public+ip+address
//https://codingvision.net/c-how-to-get-external-ip-address
String address = "";
switch(mode)
{
case 0:
using (WebClient client = new WebClient())
{
address = client.DownloadString("https://api.ipify.org/");
}
break;
case 2:
address = new WebClient().DownloadString(@"http://icanhazip.com").Trim();
break;
case 1:
WebRequest request = WebRequest.Create("http://checkip.dyndns.org/");
using (WebResponse response = request.GetResponse())
using (StreamReader stream = new StreamReader(response.GetResponseStream()))
{
address = stream.ReadToEnd();
}
int first = address.IndexOf("Address: ") + 9;
int last = address.LastIndexOf("</body>");
address = address.Substring(first, last - first);
break;
}
return address;
}
2 thoughts on “C# 取得 機器 廣域/真實/實際 IP (Global IP/public ip/external ip address)”
提供各種程式語言取得 廣域/真實/實際 IP (Global IP/public ip/external ip address)的網站
https://www.ipify.org/
Code samples
Bash
NGS (Next Generation Shell)
Python
Ruby
PHP
Java
Perl
C#
VB.NET
NodeJS
Go
Racket
Lisp
Xojo
Scala
Javascript
jQuery
Elixir
nim
PowerShell
Lua
PureBasic
LiveCode
Objective-C
Swift
Arduino
AutoIT
SAS language