我正在使用我在Get public IP using DynDNS and WebRequest C#中找到的这段代码
获取 IP 地址。但我只是从服务器获取 IP 地址,而我需要的是来自连接到我的 Web 应用程序的用户的 IP 地址。
String direction = "";
WebRequest request = WebRequest.Create("http://checkip.dyndns.org/");
using (WebResponse response = request.GetResponse())
using (StreamReader stream = new StreamReader(response.GetResponseStream()))
{
direction = stream.ReadToEnd();
}
//Search for the ip in the html
int first = direction.IndexOf("Address: ") + 9;
int last = direction.LastIndexOf("</body>");
direction = direction.Substring(first, last - first);