我使用此代码来获取公网IP地址(感谢这个帖子如何获得上我的C#应用程序正在运行的服务器的IP地址? ):
public static string GetPublicIP()
{
try
{
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);
return direction;
}
catch (Exception ex)
{
return "127.0.0.1";
}
}
但是,没有谁是访问我的网站的事情,他们都得到了相同的IP,它是服务器公网IP,而不是当前用户的IP。
是否有可能在当前用户的上下文中运行的WebRequest,不作为服务器?
或者是,我跑内部App_Code文件这一功能使当前用户的请求无法使用,而是使用服务器方面的问题?
请帮忙!