All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
使用c#操作IBM,WebSphere,MQ_.net资料_编程技术
sql删除记录_[SQL,Server教程]
C#箴言:定义常量的两种方法_[Asp.Net教程]
Visual,C#命名空间详解_[Asp.Net教程]
Sql,server2000修复实践的一次说明_[SQL,Server教程]
使用PHP程序来保护你的flash作品_[PHP教程]
数据库人员手边系列:SQL Server安全列表_[SQL Server教程]
PHP日常开发小技巧_[PHP教程]
使用.NET自带的功能制作简单的注册码_.net资料_编程技术
sql,server视图的作用_mssql学习_编程技术
Reading number is top 10 pictures
西游日记3
壮丽的云彩2
Average female college students3
Sora aoi on twitter4
你是左脑型还是右脑型
A beautiful girl to bud2
漂亮的跳舞妹妹2
Startling Russian girl blind date scene3
无题
寒流来了
Download software ranking
网页特效实例大全
双旗镇刀客B
Unix video tutorial19
传奇私服架设教程
Boxer vs Yellow3
asp.net技术内幕
株洲本地在线棋牌游戏
Unix video tutorial4
WebService在.NET中的实战应用教学视频 → 第1集
Unix video tutorial5
delv published in(发表于) 2014/1/23 3:11:37 Edit(编辑)
ASP.NET实例:检测远程URL是否存在的三种方法_[Asp.Net教程]

ASP.NET实例:检测远程URL是否存在的三种方法_[Asp.Net教程]

ASP.NET实例:检测远程URL是否存在的三种方法_[Asp.Net教程]

本文用3种方法检测远程URL是否存在。


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


string url1 = "/";
string url2 = "/Images/logo1.gif";
Response.Write("

  • 方法1:");
    Response.Write(url1 + " 存在:" + UrlExistsUsingHttpWebRequest(url1).ToString());
    Response.Write("
  • 方法2:");
    Response.Write(url1 + " 存在:" + UrlExistsUsingSockets(url1).ToString());
    Response.Write("
  • 方法3:");
    Response.Write(url1 + " 存在:" + UrlExistsUsingXmlHttp(url1).ToString());


    Response.Write("

  • 方法1:");
    Response.Write(url2 + " 存在:" + UrlExistsUsingHttpWebRequest(url2).ToString());
    Response.Write("
  • 方法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;
    }
    }







  • 添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


    Disclaimer Privacy Policy About us Site Map

    If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
    Copyright ©2011-
    uuhomepage.com, Inc. All rights reserved.