如何获取有效的url

本文关键字:有效 url 获取 何获取 | 更新日期: 2023-09-27 18:20:39

可能重复:
C#如何检查URL是否存在/有效?

关于C#vs2010的工作。

想检查url是否有效。假设我有三个类似www.google.com,www.ggg.com,www.gef.com . 的url

当我在www.google.com上浏览时=如果页面可用,那么我想要一个回复,这个页面是有效的,url也是有效的。

当我在www.ggg.com或www.gef.com上浏览时=如果页面不可用,那么我想要一个响应,此页面无效,url无效。

有可能吗?有什么想法或建议可以解决这个问题吗?提前感谢,如果有任何疑问请询问。

如何获取有效的url

.NET:检查URL';s的响应状态代码?

第一个答案将返回"url状态"。然后使用if condition=="400"检查返回状态。。等

一个例子;


List< string > urls = new List< string >
urls.add("www.google.com");
urls.add("www.ggg.com");

foreach(var url in urls) { //cast string to HttpResponse will need here... if( GetHeaders(url).ToString() == "400" ) MessageBox.Show(url + " status code is 400"); }

Something like that...

I think same question is answered already

Check this below link

C# How can I check if a URL exists/is valid?

Look into webresponse:

http://msdn.microsoft.com/en-us/library/system.net.webresponse.aspx

you can do something with that response I suppose.

You can validate url with Tegular Expression; Please have a look on below links: http://madskristensen.net/post/Validate-a-URL-using-regular-expressions.aspx http://www.webpronews.com/validating-a-url-with-regular-expressions-2006-10

You can do so by using webrequest and webresponse.

Have a look at this SO question.

Please take a look on WebClient and Uri classes.

With WebClient class (from System.Net namespace) you can check whether resource referred by Url is available:

bool resourceAvailable = false;
WebClient webClient = new WebClient();
try
{
    string pageContent = webClient.DownloadString("http://www.someResource.com");
    resourceAvailable = !String.IsNullOrEmpty(pageContent);
}
catch (WebException) { }
// then you can perform actions depending on value of resourceAvailable flag (variable)

使用Uri类,您可以检查url地址的格式是否正确:

Uri.IsWellFormedUriString("http://www.someAddress.com", UriKind.Absolute); // will return true
Uri.IsWellFormedUriString("not an uri", UriKind.Absolute); // will return false

您也可以使用UriBuilder类来完成URL的格式化。