asp.net

位置:IT落伍者 >> asp.net >> 浏览文章

asp.net用三种方法检测远程URL存在与否


发布日期:2020年04月18日
 
asp.net用三种方法检测远程URL存在与否

种方法检测远程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;

}

}

上一篇:ASP.NET自定义输出缓存提供程序

下一篇:ASP.NET文件下载函数使用浅析