C# 如何在C语言中获取地理位置信息#

C# 如何在C语言中获取地理位置信息#,c#,C#,我已经写了一些代码,根据IP地址给出了用户的地理位置。但是,当我当时将我的电脑与USBModem连接时,它会显示错误的位置 string apiKey = System.Configuration.ConfigurationManager.AppSettings["ipInfoDbKey"]; string url = "http://api.ipinfodb.com/v3/ip-city/?ip={0}&key=" + apiKey; //string url = "http://ip

我已经写了一些代码,根据IP地址给出了用户的地理位置。但是,当我当时将我的电脑与USBModem连接时,它会显示错误的位置

string apiKey = System.Configuration.ConfigurationManager.AppSettings["ipInfoDbKey"];
string url = "http://api.ipinfodb.com/v3/ip-city/?ip={0}&key=" + apiKey;
//string url = "http://ipinfodb.com/ip_query.php?ip={0}&timezone=false";
url = String.Format(url, ip);
var result = XDocument.Load(url);
var location = (from x in result.Descendants("Response")
select new LocationInfo
{
     City = (string)x.Element("City"),
     RegionName = (string)x.Element("RegionName"),
     Country = (string)x.Element("CountryName"),
     ZipPostalCode = (string)x.Element("CountryName"),
     Position = new LatLong
     {
          Lat = (float)x.Element("Latitude"),
          Long = (float)x.Element("Longitude")
     }
}).First();
return location;

我已经围绕IpAddress做了一些工作。我注意到一件事,当我在连接任何USBModem时,它使用调制解调器IP地址而不是我的pc。您能告诉我,当pc使用pc IPaddress连接到调制解调器时,如何获得地理位置吗

当您通过调制解调器连接时,您需要计算出外部IP地址,以使现有代码正常工作。为什么不调用类似的函数并解析返回字符串

比如:

var req = new HTTPGet();
req.Request("http://checkip.dyndns.org");
string[] resp = req.ResponseBody.Split(':');
string ip = resp[1];
它应该将外部IP地址作为字符串提供给现有代码


您可以从

获得HTTPGet感谢您的回复,您能告诉我如何在mvc3应用程序中使用var req=new HTTPGet(),因为它会给我带来错误抱歉,伙计,我没有意识到我在未完成之前提交了此文件。你可以从你那里得到HTTPGet通过使用这个我可以得到我的IP,即使我连接了外部USB调制解调器,谢谢Richard的正确指导。