熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> ASP編程 >> 正文

asp.net用三種方法檢測遠程URL存在與否

2022-06-13   來源: ASP編程 

  用種方法檢測遠程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
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.