如何检查有效的URL地址
本文关键字:有效 URL 地址 检查 何检查 | 更新日期: 2023-09-27 17:58:49
我有一个简单的代码,可以获取url路径并重定向到这个url:
private void Redirect(String path)
{
Uri validatedUri = null;
var result = Uri.TryCreate(HelpURL + path, UriKind.Absolute, out validatedUri);
if (result&&validatedUri!=null)
{
var wellFormed = Uri.IsWellFormedUriString(HelpURL + path, UriKind.Absolute);
if(wellFormed)
{
Response.Write("Redirect to: " + HelpURL + path);
Response.AddHeader("REFRESH", "1;URL=" + HelpURL + path);
}
else //error
{
Response.Write(String.Format("Validation Uri error!", path));
}
}
else
{
Response.Write(String.Format("Validation Uri error!", path));
}
}
Url示例:http://web-server/SomeSystemindex.html
。它不是有效地址,但是:在我的代码result
是真的,wellFormed
也是真的!
如何验证url地址?
对于这种情况,p.S.HelpUrl+path=http://web-server/SomeSystemindex.html
。HelpUrl所在的位置http://web-server/SomeSystem',路径=index.html
附言:我按照Martin说的做——创建连接并检查状态代码。
HttpWebRequest req = WebRequest.Create(HelpURL + path) as HttpWebRequest;
req.UseDefaultCredentials = true;
req.PreAuthenticate = true;
req.Credentials = CredentialCache.DefaultCredentials;
var statusCode= ((HttpWebResponse)req.GetResponse()).StatusCode;
if (statusCode == HttpStatusCode.NotFound)
isValid = false;
else if (statusCode == HttpStatusCode.Gone)
isValid = false;
else
{
isValid = true;
}
据我所知,确定地址是否有效的唯一方法是打开连接。如果连接有效,则地址有效。否则,连接无效。有一些技巧可以过滤掉坏的URL,但要知道地址是否有效,你需要打开一个连接。
一个例子已经发布在StackOverflow这里
或者在这里:
URL url;
URL wrongUrl;
try {
url = new URL("http://google.com");
wrongUrl = new URL( "http://notavalidurlihope.com");
HttpURLConnection con = (HttpURLConnection ) url.openConnection();
System.out.println(con.getResponseCode());
HttpURLConnection con2 = (HttpURLConnection ) wrongUrl.openConnection();
System.out.println(con2.getResponseCode());
} catch (IOException e) {
System.out.println("Error connecting");
}
注意:之后一定要断开
输出:
200
Error connecting
这个简单的助手方法使用regex来确保网站URL的格式正确。如果有空白(这很重要),它也会失败。
以下URL的通行证:
谷歌
www.google.com
http://google.com
http://www.google.com
https://google.com/test/test
https://www.google.com/test
它在以下情况下失败:
www.google.com/a bad path with white space/
下面是我创建的辅助方法:
public static bool ValidateUrl(string value, bool required, int minLength, int maxLength)
{
value = value.Trim();
if (required == false && value == "") return true;
if (required && value == "") return false;
Regex pattern = new Regex(@"^(?:http(s)?:'/'/)?['w.-]+(?:'.['w'.-]+)+['w'-'._~:/?#[']@!'$&''(')'*'+,;=.]+$");
Match match = pattern.Match(value);
if (match.Success == false) return false;
return true;
}