[英]determine url is true or false
I use ASP.NET with javascript , 我将ASP.NET与javascript结合使用,
I have a question in my code 我的代码有问题
I need to determine url exists or not 我需要确定网址是否存在
url="http://www.404.com"
If url == exists then
{
}
else if url == not then
{
}
If you want to do it in C# try this out, it will validate any url for you 如果您想用C#来做,请尝试一下,它将为您验证所有url。
private bool ValidateUrl(string url)
{
try
{
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
request.Method = "HEAD";
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
return (response.StatusCode == HttpStatusCode.OK);
}
catch
{
return false;
}
}
You can use C# Ping and PingReply classes. 您可以使用C#Ping和PingReply类。
private bool ValidateUrl(string url)
{
try
{
Ping x = new Ping();
int timeout=500;
PingReply reply = x.Send(url,timeout);
if(reply.Status == IPStatus.Success)
{
return true;
}
else
{
return false;
}
}
catch
{
return false;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.