最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
用c#判断远程文件是否存在
时间:2022-07-02 10:55:48 编辑:袖梨 来源:一聚教程网
public static bool IsExist(string uri)
{
HttpWebRequest req = null;
HttpWebResponse res = null;
try
{
req = (HttpWebRequest)WebRequest.Create(uri);
req.Method = "HEAD";
req.Timeout = 100;
res = (HttpWebResponse)req.GetResponse();
return (res.StatusCode == HttpStatusCode.OK);
}
catch
{
return false;
}
finally
{
if (res != null)
{
res.Close();
res = null;
}
if (req != null)
{
; req.Abort();
req = null;
}
}
}
//2:
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);
}
//3:
private bool UrlExistsUsingSockets(string url)
{
if (url.StartsWith("http://")) url = url.Remove(0, "http://".Length);
try
{
System.Net.IPHostEntry ipHost =System.Net.Dns.GetHostEntry(url);// System.Net.Dns.Resolve(url);
return true;
}
catch (System.Net.Sockets.SocketException se)
{
System.Diagnostics.Trace.Write(se.Message);
return false;
}
}
相关文章
- bilibili官方入口-b站视频官网首页直达 12-14
- 中国移动139邮箱官网-139邮箱官方登录入口 12-14
- 羞羞漫画APP全平台下载官网-安卓/iOS/鸿蒙极速安装入口 12-14
- 学信网学籍信息查询入口-在线查询学籍状态 12-14
- 死神BLEACH手游官网入口-死神BLEACH手游一键登录入口 12-14
- 樱花动漫官方正版2025官网入口-樱花动漫正版官网网址一键下载 12-14