asp.net下檢測遠(yuǎn)程URL是否存在的三種方法
更新時間:2009年12月13日 02:33:58 作者:
檢測遠(yuǎn)程URL是否存在的三種方法,需要的朋友可以參考下。
復(fù)制代碼 代碼如下:
private void Page_Load(object sender, System.EventArgs e)
{
string url1 = "http://s.jb51.net/";
string url2 = "http://m.fzitv.net/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;
}
}
您可能感興趣的文章:
- asp.net下通過泛解析和偽靜態(tài)實現(xiàn)二級域名的實現(xiàn)方法
- asp.net獲取當(dāng)前網(wǎng)址url的各種屬性(文件名、參數(shù)、域名 等)的代碼
- asp.net用url重寫URLReWriter實現(xiàn)任意二級域名 高級篇
- asp.net用url重寫URLReWriter實現(xiàn)任意二級域名 新
- asp.net 通過指定IP地址得到當(dāng)前的網(wǎng)絡(luò)上的主機(jī)的域名
- asp.net用url重寫URLReWriter實現(xiàn)任意二級域名
- asp.net下用url重寫URLReWriter實現(xiàn)任意二級域名的方法
- 在ASP.NET里得到網(wǎng)站的域名
- asp.net實現(xiàn)中英文多域名檢測的方法
相關(guān)文章
asp.net中一次性動態(tài)綁定多個droplistdown
asp.net中一次性動態(tài)綁定多個droplistdown的實現(xiàn)代碼,需要的朋友可以參考下。2011-10-10
.Net下二進(jìn)制形式的文件(圖片)的存儲與讀取詳細(xì)解析
以下是對.Net下二進(jìn)制形式的文件(圖片)的存儲與讀取進(jìn)行了詳細(xì)的分析介紹,需要的朋友可以過來參考下2013-09-09
用C#中的params關(guān)鍵字實現(xiàn)方法形參個數(shù)可變
個人認(rèn)為,提供params關(guān)鍵字以實現(xiàn)方法形參個數(shù)可變是C#語法的一大優(yōu)點。在方法形參列表中,數(shù)組類型的參數(shù)前加params關(guān)鍵字,通??梢栽谡{(diào)用方法時代碼更加精練2012-01-01
asp.net下用Aspose.Words for .NET動態(tài)生成word文檔中的數(shù)據(jù)表格的方法
導(dǎo)出word 文檔,要求這個文檔的格式不是固定的,用戶可以隨便的調(diào)整,導(dǎo)出內(nèi)容中的數(shù)據(jù)表格列是動態(tài)的,例如要求導(dǎo)出姓名和性別,你就要導(dǎo)出這兩列的數(shù)據(jù),而且這個文檔不是導(dǎo)出來之后再調(diào)整而是導(dǎo)出來后已經(jīng)是調(diào)整過了的。2010-04-04
利用.net控件實現(xiàn)下拉導(dǎo)航菜單制作的具體方法
這篇文章介紹了利用.net控件實現(xiàn)下拉導(dǎo)航菜單制作的具體方法,有需要的朋友可以參考一下,希望對你有所幫助2013-07-07
Global.asax的Application_BeginRequest實現(xiàn)url重寫無后綴的代碼
本文為大家詳細(xì)介紹下利用Global.asax的Application_BeginRequest 實現(xiàn)url重寫其無后綴,具體核心代碼如下,有需求的朋友可以參考下,希望對大家有所幫助2013-08-08

