用種方法檢測遠程URL是否存在
private void Page_Load(object sender SystemEventArgs e)
{
string url = /;
string url = ;
ResponseWrite(<li>方法);
ResponseWrite(url + 存在 + UrlExistsUsingHttpWebRequest(url)ToString());
ResponseWrite(<li>方法);
ResponseWrite(url + 存在 + UrlExistsUsingSockets(url)ToString());
ResponseWrite(<li>方法);
ResponseWrite(url + 存在 + UrlExistsUsingXmlHttp(url)ToString());
ResponseWrite(<li>方法);
ResponseWrite(url + 存在 + UrlExistsUsingHttpWebRequest(url)ToString());
ResponseWrite(<li>方法);
ResponseWrite(url + 存在 + UrlExistsUsingXmlHttp(url)ToString());
}
private bool UrlExistsUsingHttpWebRequest(string url){
try
{
SystemNetHttpWebRequest myRequest = (SystemNetHttpWebRequest)SystemNetWebRequestCreate(url);
myRequestMethod = HEAD;
myRequestTimeout = ;
SystemNetHttpWebResponse res = (SystemNetHttpWebResponse)myRequestGetResponse();
return (resStatusCode == SystemNetHttpStatusCodeOK);
}
catch (SystemNetWebException we)
{
SystemDiagnosticsTraceWrite(weMessage);
return false;
}
}
private bool UrlExistsUsingXmlHttp(string url)
{
//注意此方法需要引用Msxmldll
MSXMLXMLHTTP _xml();
_xmlhttpopen(HEAD url false null null);
_xmlhttpsend();
return (_xmlhttpstatus == );
}
private bool UrlExistsUsingSockets(string url)
{
if (urlStartsWith(//)) url = urlRemove( //Length);
try
{
SystemNetIPHostEntry ipHost = SystemNetDnsResolve(url);
return true;
}
catch (SystemNetSocketsSocketException se)
{
SystemDiagnosticsTraceWrite(seMessage);
return false;
}
}
From:http://tw.wingwit.com/Article/program/ASP/201311/21653.html