我想尝试使用ServerVariables检查IP [" REMOTE_ADDR&QUOT]但它一直返回127.0.0.1 [英] I want to try to check the IP using ServerVariables["REMOTE_ADDR"] but it keeps return 127.0.0.1

查看:115
本文介绍了我想尝试使用ServerVariables检查IP [" REMOTE_ADDR&QUOT]但它一直返回127.0.0.1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想尝试测试当前的IP地址找到当前国家

所以我尝试这个code ...
HttpContext.Current.Request.ServerVariables [REMOTE_ADDR];

但它总是返回127.0.0.1

是否有任何其他code,我可以得到像61.90.25.10互联网上我的IP地址
而非本地之一。


解决方案

  HttpContext.Current.Request.UserHostAddress;

  HttpContext.Current.Request.ServerVariables [REMOTE_ADDR];

要拿到机器的IP地址,而不是代理使用以下code

  HttpContext.Current.Request.ServerVariables [HTTP_X_FORWARDED_FOR];

但是,作为你的这个应用程序脱机使用,您将永远得到你的电脑的IP地址,因为这是IP的服务器将在另一个词看你不打算路由器之外,连接后面的服务器上,以便您的外部IP是没有使用:

如果您对运行中的服务器在线测试你的应用程序并尝试打印出来:

 的Response.Write(IP地址);
的Response.Write(IP:+ HttpContext.Current.Request.UserHostAddress);
的Response.Write(IP1:+ HttpContext.Current.Request.ServerVariables [REMOTE_ADDR]);
的Response.Write(IP2:+ HttpContext.Current.Request.ServerVariables [HTTP_X_FORWARDED_FOR]);

您会看到你有公网IP,如果使用反向代理到达您的PC一样的CloudFlare请务必使用 HTTP_X_FORWARDED_FOR 否则,您可以使用REMOTE_ADDR之前,请照顾或userhostaddress。

I want to try testing the current ip address to find current country

so i try this code... HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];

but it is always return 127.0.0.1'

Is there any other code that i can get my ip address on internet like 61.90.25.10 instead of the local one.

解决方案

HttpContext.Current.Request.UserHostAddress;

or

HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];

To get the IP address of the machine and not the proxy use the following code

HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];

But as your using this application offline you will always receive the IP address of your PC because that’s the IP the server will see in another word your not going outside your router and connecting back on the server so your external ip is not been used:

If you test your application on running server online and try to print out:

Response.Write("IP Address:");
Response.Write("IP: " + HttpContext.Current.Request.UserHostAddress);
Response.Write("IP1: " + HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]);
Response.Write("IP2: " + HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]);

You will see the public ip you have, take care if your using reverse proxy before reaching your PC like cloudflare Make sure to use the HTTP_X_FORWARDED_FOR Otherwise you can use the remote_addr or userhostaddress.

这篇关于我想尝试使用ServerVariables检查IP [" REMOTE_ADDR&QUOT]但它一直返回127.0.0.1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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