65.9K
CodeProject 正在变化。 阅读更多。
Home

Skype 的地理位置

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.77/5 (10投票s)

2007 年 1 月 19 日

3分钟阅读

viewsIcon

51964

downloadIcon

736

本文介绍了如何获取用户的公共IP地址和地理位置,并将其添加到Skype个人资料中。

Screenshot - SkypeGeoLocation.png

引言

在一个跨国公司工作真是一件乐事……

就像我们公司的一些朋友一样,你可能希望向你的Skype好友透露你当前的位置。这很容易做到,你只需将其添加到你的姓名或描述中,每个人都能看到你在哪里。但如果你经常旅行,就会出现问题;你的位置标签很可能与实际位置不符。

由于这种情况偶尔会发生,我发现这是一个很有趣的概念;足够有趣,值得去解决它。

那么,如何确定自己的位置呢?除了在你的机器上安装GPS,我建议检查你的IP地址,并根据可用的数据库之一进行转换。

思路如下。

IP地址发现

首先,如何确定我的IP地址?乍一看这似乎很简单,查询你的网络适配器并获取IP,对吧?但更可能的是,你位于NAT之后,而像10.0.0.2或192.168.0.2这样的地址(私有地址)一点帮助都没有。

因此,我们需要查看网络上可见的地址是什么。有趣的是,有很多服务可以告诉你,但可能最容易使用的是http://whatismyip.org/

public static string WhatIsMyIp()
{
    // Create a new 'Uri' object with the specified string.
    Uri myUri = new Uri("http://www.whatismyip.org/");

// Create a new request to the above mentioned URL.    
HttpWebRequest webRequest = (HttpWebRequest)HttpWebRequest.Create(myUri);
webRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; 
    SLCC1; .NET CLR 2.0.50727)";

using (WebResponse webResponse = webRequest.GetResponse())
{
    using (StreamReader reader  = new StreamReader(
        webResponse.GetResponseStream()))
    {
        // the response is actually the ip address the site is seeing
        return reader.ReadLine();
    }
}

return string.Empty;
}

看起来可以完成这项工作。

现在是IP到地理位置的转换

奇怪的是,很少有服务可以做这样的事情。最初我使用了MaxMind的GeoIP Lite City,但是向每个想要使用它的人推送10MB的数据库并不是一件好事。这对你来说可能是一个不错的解决方案,如果文件大小不是问题,我鼓励你尝试一下。

我继续寻找,有趣的是,我找到了一项非常完美的服务。GeoIP Tool

它不仅可以自动检测IP,还可以将其与地理位置数据库匹配。现有的代码很快就被一个简单的代码替换了。

public static GeoLocation WhatIsMyGeoLocation()
{
    // Create a new request to the geoiptool.com.
    // You can also retrieve the location 
    // for an arbitrary ip by providing the IP= parametetr
    HttpWebRequest myWebRequest = (HttpWebRequest)HttpWebRequest.Create(
        new Uri("http://geoiptool.com/data.php"));
myWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0;
     SLCC1; .NET CLR 2.0.50727)";

//fetch the document 
using(HttpWebResponse webResponse = (HttpWebResponse)(
    myWebRequest.GetResponse()))
{
    using (StreamReader reader = new StreamReader(
        webResponse.GetResponseStream()))
    {

        // parsing time
        XmlDocument document = new XmlDocument();
        document.Load(reader);

        // the marker is not a root so I suppose it is probable that 
        // it could support more than a single location for a given IP
        XmlNodeList nodes = document.GetElementsByTagName("marker");

        if (nodes.Count > 0)
        {
            // let's just assume that the first address 
            // is the most likely one to use.
            XmlElement marker = nodes[0] as XmlElement;

            if (marker != null)
            {
                // create an instance of our location class with the
                // data from the server response
                GeoLocation response = new GeoLocation();
                response.city = marker.GetAttribute("city");
                response.country = marker.GetAttribute("country");
                response.code = marker.GetAttribute("code");
                response.host = marker.GetAttribute("host");
                response.ip = marker.GetAttribute("ip");
                response.latitude = marker.GetAttribute("lat");
                response.lognitude = marker.GetAttribute("lng");
                return response;
            }
        }
    }
}

// this code would only be reached if something went wrong 
// no "marker" node perhaps?
return null;
}

Skype部分

这部分相当简单直接,你真正需要做的就是安装Skype(确保安装最新版本,甚至一些早期的3.0版本似乎都不能让应用程序100%工作,或者根本无法工作)并将它的COM接口导入到Visual Studio中。然后,你可以通过Interop.SKYPE4COMLib来使用它。

你可能想参考Skype API,但我发现即使不做任何广泛的阅读也很容易上手。

using SKYPE4COMLib;

namespace SkypeGeoLocation
{
public partial class MainForm : Form
{
    private static Skype skype = new SkypeClass();

    ...

        public static string SkypeName{
            get
            {
                return skype.CurrentUserProfile.FullName;
            }
            set {
                if (skype.CurrentUserProfile.FullName!= value)
                {
                    skype.CurrentUserProfile.FullName = value;
                }
            }
        }

        public static string SkypeDescription{
            get
            {
                return skype.CurrentUserProfile.MoodText;
            }
            set
            {
                if (skype.CurrentUserProfile.MoodText != value)
                {
                    skype.CurrentUserProfile.MoodText = value;
                }
            }
        }

        // ...

        public static void InitializeStructures(bool retryFailedRequests)
        {
            // ...

            // initialize skype
            if (!skype.Client.IsRunning)
            {
                skype.Client.Start(true, true);
            }

        }
}

融合 - 应用程序

该应用程序帮助你在你的名字中维护你的位置标签,以便每当你前往不同的城市时,你的Skype姓名/描述都会反映出来。

免责声明:该应用程序使用GeoIP Tool作为IP和地理位置的来源。访问他们的网站以获取各种基于Web的地理位置工具。

它不包含任何跟踪功能,除了允许你维护你的位置标签。应用程序甚至不会自行更改你的位置标签,而是会通知你你的位置已更改,并允许你只需按一下按钮即可更改标签。

用法

在你的Skype帐户中设置你的姓名或描述,使其包含[],或者按下应用程序中的“添加位置标签到……”。每当应用程序发现时,它都会检查括号中的文本是否与你当前的位置匹配。如果不匹配,它会建议一个新的位置,并将相关字段突出显示为红色。你可以设置此操作在每次启动时执行,但是,只有当你的位置实际发生变化时,你才会收到通知。

© . All rights reserved.