1

I have simple code , wich get url path and redirect to this 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));
        }                                        
    }

Example of Url:http://web-server/SomeSystemindex.html. It is not valid address, but: at my code result is true, wellFormed is true too!

How to validate url address?

P.S. HelpUrl+path=http://web-server/SomeSystemindex.html for this case. Where HelpUrl is 'http://web-server/SomeSystem', and path=index.html

P.P.S. I do as Martin says- create connection and check the status code.

  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;
                }
Admiral Land
  • 2,304
  • 7
  • 43
  • 81
  • This is indeed a valid URL? How are you saying it's not valid? – Rahul Jul 26 '16 at 08:16
  • @EliArbel, i mean that browser can not navigate to this URL.. – Admiral Land Jul 26 '16 at 08:16
  • 1
    Side note: When an url is passed into a parameter you should always be vary of url injection. I note that this is a private method, so it may not be a problem. But just saying. ;) – smoksnes Jul 26 '16 at 08:16
  • Unreachable is not the same as invalid. You need to actually make a call to the url to find out if it's unreachable or not. – J. Steen Jul 26 '16 at 08:16
  • @smoksnes,yes it is configuration option... but if hacker can go to server and change congif file..it is epic fail – Admiral Land Jul 26 '16 at 08:18
  • 1
    @AdmiralLand - Yeah, and when that happens you probably have some more major problems... :) – smoksnes Jul 26 '16 at 08:29

2 Answers2

4

As far as I know, the only way to determine whether an address is valid or not, is by opening a connection. If the connection lives, the address is valid. If not, the connection is not valid. There are some tricks to filter out bad URL's, but to know whether an adress is valid, you need to open a connection.

An example has already been posted on StackOverflow here

Or here:

    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");
    }

Note: Do disconnect afterwards

output:

200
Error connecting
Community
  • 1
  • 1
MartW
  • 131
  • 1
  • 10
  • @Martin , yes, if you can give code snippet to check connection- i think it willbe a valid response :) – Admiral Land Jul 26 '16 at 08:20
  • 1
    You should post a brief code sample anyway, since that would *actually* answer the question. – J. Steen Jul 26 '16 at 08:20
  • Fair enough, will do in the future – MartW Jul 26 '16 at 08:34
  • @MartijnWoudstra, unfortunatelly, i try to use HttpWebRequest webRequestObject = (HttpWebRequest)HttpWebRequest.Create(HelpURL + path); and is works this any adrress.. and when i try to GetResponse - is throw exception at any way (valid or not valid url) – Admiral Land Jul 26 '16 at 11:07
1

This simple helper method uses regex to ensure that a website URL is in correct format. It will also fail if there is any white space (which is important).

The following URL's pass:

google.com

www.google.com

http://google.com

http://www.google.com

https://google.com/test/test

https://www.google.com/test

It fails on:

www.google.com/a bad path with white space/

Below is the helper method I created:

    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;
    }
Andrew Reese
  • 854
  • 1
  • 11
  • 27