如何获得外部IP地址 [英] How get external IP address

查看:100
本文介绍了如何获得外部IP地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





Hi,

public void GetLoginIP()
    {
        try
        {
            String strIP = "";
            WebRequest request = WebRequest.Create("http://checkip.dyndns.org/");
            using (WebResponse response = request.GetResponse())
            using (StreamReader stream = new StreamReader(response.GetResponseStream()))
            {
                strIP = stream.ReadToEnd();
            }
            int first = strIP.IndexOf("Address: ") + 9;
            int last = strIP.LastIndexOf("</body>");
            strIP = strIP.Substring(first, last - first);

            Session["IP"] = strIP;
        }
        catch(Exception Ex)
        {

        }

    }





在德国地区托管代码时,网站运行速度太慢...我怎样才能让它快速运行



The site is too slow when working above code when it host in Germany region...how can i make it work fast

推荐答案

我认为这可能是网络延迟问题。所以你可以在应用程序启动时调用上面的functionlity并存储在某个地方..所以你不需要每次都获取它







i think this may be the network latency issue. so u can invoke above functionlity once on application startup and stores somewhere.. so u dont need to fetch it every time



public static void GetLoginIP()
    {
        try
        {
            if(Session["IP"] == null)
{
            String strIP = "";
            WebRequest request = WebRequest.Create("http://checkip.dyndns.org/");
            using (WebResponse response = request.GetResponse())
            using (StreamReader stream = new StreamReader(response.GetResponseStream()))
            {
                strIP = stream.ReadToEnd();
            }
            int first = strIP.IndexOf("Address: ") + 9;
            int last = strIP.LastIndexOf("</body>");
            strIP = strIP.Substring(first, last - first);

            Session["IP"] = strIP;
}
        }
        catch(Exception Ex)
        {

        }

    }


这篇关于如何获得外部IP地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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