-3

How come the following URL is valid ?

var testURL = "http://" + "ggggggggg" +"/" 
if(Uri.IsWellFormedUriString(testURL, UriKind.RelativeOrAbsolute))
{
// it comes here
}
casillas
  • 16,351
  • 19
  • 115
  • 215
  • Well-formed just means the string "looks like" a good URL. It has nothing to do with whether or not anything actually exists at that URL. – Joel Coehoorn Jul 10 '17 at 22:09
  • isn't [http://yourcomputername/](http://yourcomputername/) a "valid" url? – Gian Paolo Jul 10 '17 at 22:10
  • @JoelCoehoorn, then how can I test url iswellformed? whats is your suggestion? – casillas Jul 10 '17 at 22:10
  • @hotspring: What exactly are you trying to accomplish here? Do you want some way to tell if there's actually a *page* at that URL? Validating the string alone isn't going to do that, you'd need to send an actual HTTP request to the URL and examine the result. – David Jul 10 '17 at 22:12
  • Please read URI Rfc (https://tools.ietf.org/html/rfc3986) to clarify your question. It is very unclear why you expect anything different from "true" for `http://ggggggggg`. – Alexei Levenkov Jul 10 '17 at 22:14
  • You just did test that it's well-formed. `http://ggggggggg/` is a perfectly legal and well-formed URL. – Joel Coehoorn Jul 10 '17 at 22:14
  • to test if a string is a well formed URL, you can use `Uri.IsWellFormedUriString` as you are doing. To test if at that address there is really a web server answering your http request.. send an http request to it and examine the result, as @David suggests – Gian Paolo Jul 10 '17 at 22:15
  • Yes, David. I am trying to validate whether or not it is a wellformed url for service connection – casillas Jul 10 '17 at 22:15
  • @hotspring: You're continuing to be unclear about your intention here. If you are just testing if the URL string is *well formed*, your code already does that. If you want to test if the URL *has a page* then you need to make an HTTP request to it. Which is it? – David Jul 10 '17 at 22:16
  • (Note that both variants outlined by @David have plenty of duplicates, so clearly you are looking for something else). – Alexei Levenkov Jul 10 '17 at 22:16
  • wellformed means "something that can be a valid url", e.g. `http://google.com`. vs something invalid such, e.g. `http://site with spaces.com` – Gian Paolo Jul 10 '17 at 22:17
  • I want to test whether or not user enters the right input. I do not expect user to put something weird such as "gggggggg", I want to validate what he enters, at least right format. – casillas Jul 10 '17 at 22:17
  • YEs @GianPaolo, that is what I am looking for. – casillas Jul 10 '17 at 22:18
  • 1
    [http://ggggggggg/](http://ggggggggg/) is a right format for an url – Gian Paolo Jul 10 '17 at 22:18
  • 2
    @hotspring: Define "something weird" and "right format". Because the URL you are testing ***is a valid format for a URL***. If you have some custom logic you want to implement, the framework tools aren't going to know about that. – David Jul 10 '17 at 22:18
  • @hotspring: `string.Contains()` is the method you're looking for then. Something like: `testURL.Contains(".com")` Note that this will also validate true for something like: `http://.completelyfakeurl` – David Jul 10 '17 at 22:19
  • However, what if I am hosting the service on my local machine, then it does not require to have `.com`... but is there a generic solution to check url validation that could be global or local? such as `abc.com/myservice.svc` is right, but `abc.com\myservice.svc` is not – casillas Jul 10 '17 at 22:23
  • 3
    @hotspring: There is no solution which would perform an intuition judgement that you will personally and subjectively agree with in all cases. There is only a generic solution which determines if the URL is well-formed for the format of a URL, which is what you're currently using. Any custom rules you want to implement for your particular logic, you'll need to implement yourself. Custom logic that you've made up isn't going to be in any framework. – David Jul 10 '17 at 22:29

1 Answers1

2

Well-formed just means the string "looks like" a good URL. It has nothing to do with whether or not anything actually exists at that URL. There isn't even a requirement for a valid top level domain, or any top level domain at all. You only need valid host name, which can be simpler.

In this case, the URL http://ggggggggg/ is valid and well-formed, because ggggggg is perfectly valid for the host portion of a URL. It would be perfectly legal to set up a web host on your local network with that name. If you really want to know if there is a web server at that address, then go ahead and send an http request, and wait for the response.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794