使用C#获取IP地理位置WPF [英] Get IP geolocation using C# & WPF

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

问题描述

我正在做一个模块,可以使用我使用的设备的IP地址显示我的城市,州,经度和纬度。
但是,我无法理解。下面,我介绍了另一个网站的代码:

 内部GeoLoc GetMyGeoLocation()
{
尝试
{
//创建一个请求到geoiptool.com
var request = WebRequest.Create(new Uri(http://geoiptool.com/data.php))as HttpWebRequest的;


if(request!= null)
{
//设置请求用户代理
request.UserAgent =Mozilla / 4.0(compatible; MSIE 7.0; Windows NT 6.0; SLCC1; .NET CLR 2.0.50727);

//获取响应
使用(var webResponse =(request.GetResponse()as HttpWebResponse))
if(webResponse!= null)
using(var reader = new StreamReader(webResponse.GetResponseStream()))
{

//获取XML文档
var doc = new XmlDocument();
doc.Load(reader);

//现在我们解析XML文档
var nodes = doc.GetElementsByTagName(marker);

Guard.AssertCondition(nodes.Count> 0,nodes,new object());
//确保循环前有节点
// if(nodes.Count> 0)
// {
//获取第一个响应
var marker = nodes [0]作为XmlElement;

Guard.AssertNotNull(marker,marker);

//获取数据并返回
_geoLoc.City = marker.GetAttribute(city);
_geoLoc.Country = marker.GetAttribute(country);
_geoLoc.Code = marker.GetAttribute(code);
_geoLoc.Host = marker.GetAttribute(host);
_geoLoc.Ip = marker.GetAttribute(ip);
_geoLoc.Latitude = marker.GetAttribute(lat);
_geoLoc.Lognitude = marker.GetAttribute(lng);
_geoLoc.State = GetMyState(_geoLoc.Latitude,_geoLoc.Lognitude);

return _geoLoc;
//}
}
}

//只有在出现错误时才能达到此代码
//否标记节点或许?
返回新的GeoLoc();
}
catch(Exception ex)
{
throw;
}

}

所有的代码都没有问题。唯一的问题是 _geoLoc ,它会在每一个下面都提示出红线。这是什么意思?谢谢。

解决方案

正如其他人指出的,_geoLoc没有定义。尝试像这样

 内部GeoLoc GetMyGeoLocation()
{
尝试
{
//创建一个请求到geoiptool.com
var request = WebRequest.Create(new Uri(http://geoiptool.com/data.php))作为HttpWebRequest;


if(request!= null)
{
//设置请求用户代理
request.UserAgent =Mozilla / 4.0(compatible; MSIE 7.0; Windows NT 6.0; SLCC1; .NET CLR 2.0.50727);

//获取响应
使用(var webResponse =(request.GetResponse()as HttpWebResponse))
if(webResponse!= null)
using(var reader = new StreamReader(webResponse.GetResponseStream()))
{

//获取XML文档
var doc = new XmlDocument();
doc.Load(reader);

//现在我们解析XML文档
var nodes = doc.GetElementsByTagName(marker);

Guard.AssertCondition(nodes.Count> 0,nodes,new object());
//确保循环前有节点
// if(nodes.Count> 0)
// {
//获取第一个响应
var marker = nodes [0]作为XmlElement;

Guard.AssertNotNull(marker,marker);

var _geoLoc = new GeoLoc();
//获取数据并返回
_geoLoc.City = marker.GetAttribute(city);
_geoLoc.Country = marker.GetAttribute(country);
_geoLoc.Code = marker.GetAttribute(code);
_geoLoc.Host = marker.GetAttribute(host);
_geoLoc.Ip = marker.GetAttribute(ip);
_geoLoc.Latitude = marker.GetAttribute(lat);
_geoLoc.Lognitude = marker.GetAttribute(lng);
_geoLoc.State = GetMyState(_geoLoc.Latitude,_geoLoc.Lognitude);

return _geoLoc;
//}
}
}

//只有在出现错误时才能达到此代码
//否标记节点或许?
返回新的GeoLoc();
}
catch(Exception ex)
{
throw;
}
}


I am doing a module which can display my city, state, latitude and longitude, using IP address of the device I am using. However, I can't get it right. Below, there is my code which I referred to another website:

internal GeoLoc GetMyGeoLocation()
{
    try
    {
        //create a request to geoiptool.com
        var request = WebRequest.Create(new Uri("http://geoiptool.com/data.php")) as HttpWebRequest;


    if (request != null)
    {
        //set the request user agent
        request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; SLCC1; .NET CLR 2.0.50727)";

        //get the response
        using (var webResponse = (request.GetResponse() as HttpWebResponse))
            if (webResponse != null)
                using (var reader = new StreamReader(webResponse.GetResponseStream()))
                {

                    //get the XML document
                    var doc = new XmlDocument();
                    doc.Load(reader);

                    //now we parse the XML document
                    var nodes = doc.GetElementsByTagName("marker");

                    Guard.AssertCondition(nodes.Count > 0,"nodes",new object());
                    //make sure we have nodes before looping
                    //if (nodes.Count > 0)
                    //{
                        //grab the first response
                        var marker = nodes[0] as XmlElement;

                        Guard.AssertNotNull(marker, "marker");

                        //get the data and return it
                        _geoLoc.City = marker.GetAttribute("city");
                        _geoLoc.Country = marker.GetAttribute("country");
                        _geoLoc.Code = marker.GetAttribute("code");
                        _geoLoc.Host = marker.GetAttribute("host");
                        _geoLoc.Ip = marker.GetAttribute("ip");
                        _geoLoc.Latitude = marker.GetAttribute("lat");
                        _geoLoc.Lognitude = marker.GetAttribute("lng");
                        _geoLoc.State = GetMyState(_geoLoc.Latitude, _geoLoc.Lognitude);

                        return _geoLoc;
                    //}
                }
    }

    // this code would only be reached if something went wrong 
    // no "marker" node perhaps?
    return new GeoLoc();
}
catch (Exception ex)
{
    throw;
}

}

All the code has no problem. The only problem would be the _geoLoc, it keep prompt out red lines below each of them. What does it mean? Thank you.

解决方案

As others have pointed out, _geoLoc isn't defined. Try something like this

internal GeoLoc GetMyGeoLocation()
{
    try
    {
        //create a request to geoiptool.com
        var request = WebRequest.Create(new Uri("http://geoiptool.com/data.php")) as HttpWebRequest;


    if (request != null)
    {
        //set the request user agent
        request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; SLCC1; .NET CLR 2.0.50727)";

        //get the response
        using (var webResponse = (request.GetResponse() as HttpWebResponse))
            if (webResponse != null)
                using (var reader = new StreamReader(webResponse.GetResponseStream()))
                {

                    //get the XML document
                    var doc = new XmlDocument();
                    doc.Load(reader);

                    //now we parse the XML document
                    var nodes = doc.GetElementsByTagName("marker");

                    Guard.AssertCondition(nodes.Count > 0,"nodes",new object());
                    //make sure we have nodes before looping
                    //if (nodes.Count > 0)
                    //{
                        //grab the first response
                        var marker = nodes[0] as XmlElement;

                        Guard.AssertNotNull(marker, "marker");

                        var _geoLoc = new GeoLoc();
                        //get the data and return it
                        _geoLoc.City = marker.GetAttribute("city");
                        _geoLoc.Country = marker.GetAttribute("country");
                        _geoLoc.Code = marker.GetAttribute("code");
                        _geoLoc.Host = marker.GetAttribute("host");
                        _geoLoc.Ip = marker.GetAttribute("ip");
                        _geoLoc.Latitude = marker.GetAttribute("lat");
                        _geoLoc.Lognitude = marker.GetAttribute("lng");
                        _geoLoc.State = GetMyState(_geoLoc.Latitude, _geoLoc.Lognitude);

                        return _geoLoc;
                    //}
                }
           }

        // this code would only be reached if something went wrong 
        // no "marker" node perhaps?
        return new GeoLoc();
    }
    catch (Exception ex)
    {
        throw;
    }
}

这篇关于使用C#获取IP地理位置WPF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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