获取系统的IP [英] getting the ip of system

查看:52
本文介绍了获取系统的IP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好
我正在使用System.Net,要获取系统的IP,代码如下:

Hi All
i am using System.Net, for getting the ip of system the code is given below:

public string getip()
 {
     IPHostEntry host;
     string localIP = "?";
     host = Dns.GetHostEntry(Dns.GetHostName());
     foreach (IPAddress ip in host.AddressList)
     {
         if (ip.AddressFamily.ToString() == "InterNetwork")
         {
             localIP = ip.ToString();
         }
     } return (localIP);
 }



当我在本地主机上运行应用程序时,我得到了正确的IP,但是当我在服务器上托管此应用程序时,它给出了托管服务器的IP.

请建议我,在服务器上部署应用程序时如何获取客户端计算机的IP.

请提供样品
谢谢&关于



i am getting the right ip when i am running my application on localhost but when i am hosting this application on server, it given the ip of hosted server.

please suggest me, how to get ip of client machine when application is deploy on server.

Please provide sample if possible
Thanks & Regards

推荐答案

public static string getclientIP()
{
    string result= string.Empty;
    string ip = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
    if (!string.IsNullOrEmpty(ip))
    {
        string[] ipRange = ip.Split(',');
        int le = ipRange.Length - 1;
        result = ipRange[0];
    }
    else
    {
        result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
    }
 
    return result;
}



如果您运行,则将获得IP地址127.0.0.1.此处127.0.0.1是localhost.这意味着您正在使用PC.如果您尝试使用其他PC使用您的应用程序,您将获得一个不同的IP地址.我的意思是将您的应用程序托管在IIS或服务器上,而另一台PC必须使用您的应用程序.这样,您将获得一个不同的IP. 127.0.0.1是本地主机,即当前的IP地址.



If u run u will get the ip address 127.0.0.1.Here 127.0.0.1 is localhost. this means you are working on your PC. If you try other PC to use your app, you will get a different ip address.what i mean is host your application in your IIS or on your server and another pc must use your application. with that, you will get a different IP. 127.0.0.1 is localhost, meaning, current IP address.


http ://www.devhood.com/tutorials/tutorial_details.aspx?tutorial_id = 577 [ ^ ]



http://www.w3schools.com/asp/coll_servervariables.asp [
http://www.devhood.com/tutorials/tutorial_details.aspx?tutorial_id=577[^]



http://www.w3schools.com/asp/coll_servervariables.asp[^]

The following example demonstrates how to find out the visitor''s browser type, IP address, and more:

<html>
 <body>
 <p>
 <b>You are browsing this site with:</b>
 <%Response.Write(Request.ServerVariables("http_user_agent"))%>
 </p>
 <p>
 <b>Your IP address is:</b>
 <%Response.Write(Request.ServerVariables("remote_addr"))%>
 </p>
 <p>
 <b>The DNS lookup of the IP address is:</b>
 <%Response.Write(Request.ServerVariables("remote_host"))%>
 </p>
 <p>
 <b>The method used to call the page:</b>
 <%Response.Write(Request.ServerVariables("request_method"))%>
 </p>
 <p>
 <b>The server's domain name:</b>
 <%Response.Write(Request.ServerVariables("server_name"))%>
 </p>
 <p>
 <b>The server's port:</b>
 <%Response.Write(Request.ServerVariables("server_port"))%>
 </p>
 <p>
 <b>The server's software:</b>
 <%Response.Write(Request.ServerVariables("server_software"))%>
 </p>
 </body>
 </html>


REMOTE_ADDR HTTP_X_FORWARDED_FOR是我们想要捕获用户IP地址的两个服务器变量.
作为
REMOTE_ADDR and HTTP_X_FORWARDED_FOR are the two server variables we are interested in in order to attempt to capture a users IP address.
as
object UserIPAddress = null;
UserIPAddress = Request.ServerVariables("HTTP_X_FORWARDED_FOR");
if (string.IsNullOrEmpty(UserIPAddress)) {
	UserIPAddress = Request.ServerVariables("REMOTE_ADDR");



我们之所以选择HTTP_X_FORWARDED_FOR 值1st是因为代理服务器之类的东西,因为许多用户都在其后.如果该值不存在,我们只需抓住REMOTE_ADDR.即使这样做,有时也无法获得IP地址或所获得的信息不准确.

参考链接:-

检索用户IP地址
[ ^ ]



The reason we look for the HTTP_X_FORWARDED_FOR value 1st is because of proxy servers and things of that nature as many users are behind one. If that value is not there we just grab the REMOTE_ADDR. Even by doing this there are going to be times when we do not get an IP Address or what we get is not accurate.

Reference Link :-
Retrieve a Users IP Address
[^]


这篇关于获取系统的IP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆