【问题标题】:Client' country name not returns correctly客户的国家名称未正确返回
【发布时间】:2014-01-10 04:28:40
【问题描述】:

我正在使用http://ipaddressextensions.codeplex.com/ dll 库来检索客户所在的国家/地区,并且我正在使用以下代码。但结果总是显示“澳大利亚”,但我来自孟加拉国。有人可以使用该代码纠正我吗?

using System.Net;
using WorldDomination.Net;

string userHostIpAddress = "203.1.2.3";
IPAddress ipAddress;
if (IPAddress.TryParse(userHostIpAddress, out ipAddress))
{
    string country = ipAddress.Country(); // return value: UNITED STATES
    string iso3166TwoLetterCode = ipAddress.Iso3166TwoLetterCode(); // return value: US
}

【问题讨论】:

标签: c# asp.net ip-address whois


【解决方案1】:

这是示例源代码,其中将 IP 地址指定为常量。它不会查找您自己的 IP 地址!

string userHostIpAddress = "203.1.2.3"; // this is an Australian IP

你需要找到自己的IP并使用那个

例如How to get the IP address of the server on which my C# application is running on?中对此进行了解释

【讨论】:

  • 我只能在一行中获取公共IP地址:string ipAddress = new WebClient().DownloadString("icanhazip.com");但是当我将它作为参数发送给 userHostIpAddress 时,我得到空值作为国家名称。我也尝试将我的公共 IP 直接分配给它,然后我得到了正确的国家。这是什么问题?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-04
  • 1970-01-01
  • 2020-12-05
  • 2021-07-14
  • 2015-08-18
相关资源
最近更新 更多