我正在开发一个MVC.ASP.Net应用程序。我正在尝试查找各自国家/地区的公共IP地址。
Public static string GetIP()
{
string IPAddress = null;
IPAddress = WDA.CMS.Services.CacheService.getPublicIP;
using (System.Net.WebClient wc = new System.Net.WebClient())
{
// Take long time to respond.
IPAddress = wc.DownloadString("http://icanhazip.com/");
}
if (!string.IsNullOrEmpty(IPAddress))
{
System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex("(\\d{1,3}\\.){3}\\d{0,3}");
if (IPAddress != null)
{
if (regex.IsMatch(IPAddress))
{
IPAddress = regex.Match(IPAddress).Value.ToString();
}
else
{
IPAddress = "";
}
regex = null;
}
}
return IPAddress;
}
//如果是美国:209.105.127.206
此功能正常工作但我正在尝试改善响应时间,因为多次调用此函数。是否可以修改此函数以更快地检索结果。任何帮助/建议将受到高度赞赏。
答案 0 :(得分:1)
缓存它。即
public JsonResult Search(string term)
{
List<string> lstADUsers = new List<string>();
if (!string.IsNullOrEmpty(term))
{
using (var context = new PrincipalContext(ContextType.Domain, null, "LDAP"))
{
UserPrincipal user = new UserPrincipal(context);
using (var searcher = new PrincipalSearcher(new UserPrincipal(context)))
{
foreach (var result in searcher.FindAll().Where(m => m.SamAccountName.StartsWith(term, StringComparison.OrdinalIgnoreCase)))
{
DirectoryEntry de = result.GetUnderlyingObject() as DirectoryEntry;
string usersWithName;
if (!String.IsNullOrEmpty((String)de.Properties["samaccountname"].Value) && !String.IsNullOrEmpty((String)de.Properties["sn"].Value) && !String.IsNullOrEmpty((String)de.Properties["givenName"].Value))
{
usersWithName = de.Properties["samaccountname"].Value.ToString();
lstADUsers.Add(usersWithName);
}
}
}
}
}
return Json(lstADUsers, JsonRequestBehavior.AllowGet);
}