C#如何检查URL是否存在有效?

C#如何检查URL是否存在有效?,第1张

C#如何检查URL是否存在/有效?

您可以发出“
HEAD”
请求而不是“
GET”请求?

(编辑)-大声笑!看来我以前做过!改为Wiki,以避免指责获得销售代表。因此,要测试URL而无需下载内容:

// using MyClient from linked postusing(var client = new MyClient()) {    client.Headonly = true;    // fine, no content downloaded    string s1 = client.DownloadString("http://google.com");    // throws 404    string s2 = client.DownloadString("http://google.com/silly");}

您将

try
/
catch
在附近
DownloadString
检查错误;没有错吗 存在…


使用C#2.0(VS2005):

private bool headOnly;public bool Headonly {    get {return headOnly;}    set {headonly = value;}}

using(WebClient client = new MyClient()){    // pre as before}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/4972492.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-13
下一篇 2022-11-13

发表评论

登录后才能评论

评论列表(0条)

保存