C# 如何从浏览器获取连接的网站ip地址?

C# 如何从浏览器获取连接的网站ip地址?,c#,network-programming,ip-address,detection,C#,Network Programming,Ip Address,Detection,我正在尝试检测哪个网站用户连接到 我尝试获取tcp连接,并解析它们,例如,我尝试检测facebook。但当我注销并关闭facebook时,它仍然显示31.13.93.3(facebook的ip) 这是我的密码 public partial class Form1 : Form { static string faceIP = "31.13.93.3"; static string _targetIP,_targetPORT,_connectedWebSiteIP,_connect

我正在尝试检测哪个网站用户连接到

我尝试获取tcp连接,并解析它们,例如,我尝试检测facebook。但当我注销并关闭facebook时,它仍然显示31.13.93.3(facebook的ip)

这是我的密码

public partial class Form1 : Form
{
    static string faceIP = "31.13.93.3";
    static string _targetIP,_targetPORT,_connectedWebSiteIP,_connectedWebSitePORT = string.Empty;
    static string[] splitted = null;
    public Form1()
    {
        /* 127.0.0.1:5037:127.0.0.1:49569

         * First = 127.0.0.1
         * Second = 5037
         * Third = 127.0.01
         * Fourth = 49569
         */
        InitializeComponent();
        this.Name = "Active Tcp Connections";
        if (findFacebookIP())
        {
            MessageBox.Show("You opened or connected to facebook page!");
        }
    }
    public static bool findFacebookIP(){
        IPGlobalProperties properties = IPGlobalProperties.GetIPGlobalProperties();
        TcpConnectionInformation[] connections = properties.GetActiveTcpConnections();
        foreach (TcpConnectionInformation c in connections)
        {
            string tcpCon = string.Format("{0}:{1}", c.LocalEndPoint.ToString(), c.RemoteEndPoint.ToString());
            splitted = tcpCon.Split(':');
            _targetIP = splitted[0]; // Main Machine ip adress / local ip address (First)
            _targetPORT = splitted[1]; // Main machine port number (Second)
            _connectedWebSiteIP = splitted[2]; // (Third)
            _connectedWebSitePORT = splitted[3]; // (Fourth)
            if (_connectedWebSiteIP == faceIP)
            {
                return true;
            }
        }
        return false;
    }
    // face ip = 31.13.93.3
}
我还需要一直在后台运行它,因为这个方法只适用于打开。。您可以看到,我是用Form1()构造函数方法编写的。
谢谢您的帮助。

使用
TcpState
检查:

foreach (TcpConnectionInformation c in connections)
{
    //------------rest of your code
    if(c.State == TcpState.Closed)
         return false;
    //------------rest of your code
}

和后台运行使用

不客气。但如果使用我的解决方案解决了问题,请标记我的答案。:)对不起,这不是你的答案:)谢谢你的帮助。