本文介绍了如何获得外部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
推荐答案
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地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!