惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

The GitHub Blog
The GitHub Blog
Hugging Face - Blog
Hugging Face - Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
WordPress大学
WordPress大学
Y
Y Combinator Blog
Google DeepMind News
Google DeepMind News
大猫的无限游戏
大猫的无限游戏
Vercel News
Vercel News
V
Visual Studio Blog
Microsoft Azure Blog
Microsoft Azure Blog
MyScale Blog
MyScale Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
罗磊的独立博客
N
Netflix TechBlog - Medium
M
MIT News - Artificial intelligence
G
Google Developers Blog
阮一峰的网络日志
阮一峰的网络日志
P
Proofpoint News Feed
小众软件
小众软件
Engineering at Meta
Engineering at Meta
D
DataBreaches.Net
S
SegmentFault 最新的问题
H
Help Net Security
量子位

博客园 - 失落

第三方支付接口搜集(附下载) SelectNodes 与 XPath 配置BizTalk时提示无法连接到SQLServer上的SSODB数据库 - 失落 - 博客园 ADO Connection Strings 两台笔记本无线共享上网图解 [转]DB2 修改模式 [转]学会使用DB2指令 [转]学会使用DB2指令及故障诊断 BizTalk 宏 js javascript 详解各控件的操作 - 失落 - 博客园 JavaScript的replace方法与正则表达式结合应用讲解 - 失落 - 博客园 HTML控件事件一览表 收集一些常用的正则表达式 解javascript的caller,callee,call,apply概念 JavaScript事件的理解 javascript 事件 SQL Server 2005 如何在没有日志文件的情况下如何恢复MDF数据库文件(测试通过) 订票地址
检测远程URL是否存在的三种方法
失落 · 2007-11-08 · via 博客园 - 失落

private void Page_Load(object sender, System.EventArgs e)
{

 string url1 = "http://dotnet.aspx.cc/";
 string url2 = "http://dotnet.aspx.cc/Images/logo.gif";
 Response.Write("<li>方法1:");
 Response.Write(url1 + " 存在:" + UrlExistsUsingHttpWebRequest(url1).ToString());
 Response.Write("<li>方法2:");
 Response.Write(url1 + " 存在:" + UrlExistsUsingSockets(url1).ToString());   
 Response.Write("<li>方法3:");
 Response.Write(url1 + " 存在:" + UrlExistsUsingXmlHttp(url1).ToString());

 Response.Write("<li>方法1:");
 Response.Write(url2 + " 存在:" + UrlExistsUsingHttpWebRequest(url2).ToString());
 Response.Write("<li>方法3:");
 Response.Write(url2 + " 存在:" + UrlExistsUsingXmlHttp(url2).ToString());
}
private bool UrlExistsUsingHttpWebRequest(string url)
{
 try
 {
  System.Net.HttpWebRequest myRequest =(System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
  myRequest.Method = "HEAD";
  myRequest.Timeout = 100;
  System.Net.HttpWebResponse res = (System.Net.HttpWebResponse)myRequest.GetResponse();
  return (res.StatusCode == System.Net.HttpStatusCode.OK);
 }
 catch(System.Net.WebException we)
 {
  System.Diagnostics.Trace.Write(we.Message);
  return false;
 }
}
private bool UrlExistsUsingXmlHttp(string url)
{
 //注意:此方法需要引用Msxml2.dll
 MSXML2.XMLHTTP _xmlhttp = new MSXML2.XMLHTTPClass();
 _xmlhttp.open("HEAD",url,false,null,null);
 _xmlhttp.send("");
 return (_xmlhttp.status == 200 );
}

private bool UrlExistsUsingSockets(string url)
{
 if(url.StartsWith("http://")) url = url.Remove(0,"http://".Length);
 try
 {
  System.Net.IPHostEntry ipHost = System.Net.Dns.Resolve(url);
  return true;
 }
 catch (System.Net.Sockets.SocketException se)
 {
  System.Diagnostics.Trace.Write(se.Message);
  return false;
 }
}