How to check what is wrong in domain URL in C#
I want to updated domain URL when invalid domain enter.
Input Put of Domain: OutPut Like
1)http:/localhost:1234/------>http://localhost:1234/
2)http://localhost:1234------>http://localhost:1234/
3)http:localhost:1234/------->http://localhost:1234/
4)http:localhost:1234-------->http://localhost:1234/
5)localhost:1234/------------>http://localhost:1234/
6)localhost:1234------------->http://localhost:1234/
Also above all test cases with HTTPS
May be need add more test cases.
I have code of nopCommerece for warning but it's use only current store .
How I develop a code for enter domain is valid or not and return valid domain.
My understanding of the question is you want to take in a given URL and output a correction. At the very minumum you are looking for the string "localhost:1234". You could use a regular expression to check for the existence of this string. If true, output "http://localhost:1234/"
The regular express is "/localhost:1234/g" and can be found here: http://regexr.com/3e2n8
To check this regular expression in C# you will code:
Regex regex = new Regex(#"/localhost:1234/g");
Match match = regex.Match("http:/localhost:1234/"); // your given string
if (match.Success)
{
// your given string contains localhost:1234
}
In any domain name the following are important:
www..com:
portnumber is 80 by default
but still, to check and get the Exception, use this URL,
Best way to determine if a domain name would be a valid in a "hosts" file?
Related
I have a user input to supply website address, obviously most users have no idea what is well formatted url so I look for a website address Regex that will follow this rules:
1) www.someaddress.com - True
2) someaddress.com - True
3) http://someaddress.com - True
4) https://someaddress.com - True
5) https://www.someaddress.co.il - True
6) http://www.someaddress.com - True
I use this Regex:
[RegularExpression(#"^((http|ftp|https|www)://)?([\w+?\.\w+])+([a-zA-Z0-9\~\!\#\#\$\%\^\&\*\(\)_\-\=\+\\\/\?\.\:\;\'\,]*)?$", ErrorMessage = "Not a valid website address")]
public string SiteUrl { get; set; }
But it's useless because it allows almost every string to pass.
Please supply a data annotation answer and not answers such as:
Uri.IsWellFormedUriString
Because .net doesn't support client side validation for custom attributes.
There is a UrlAttribute to validate URLs, but it does enforce the protocol being there, which it appears you don't want.
However, the source code is available and it does use a regular expression that you can steal and modify. Modifying just the protocol portion to be optional the way you want, you get this:
^((http|ftp|https)://)?(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDC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|\?)*)?$
(Side note: I noticed that your regex allowed www://, which is suspicious. I took it out in this, but if you truly do need that, then you can add it.)
These are values I tested with:
www.someaddress.com Yes
someaddress.com Yes
http://someaddress.com Yes
https://someaddress.com Yes
https://www.someaddress.co.il Yes
cow No
hi hello.com No
this/that.com No
In the comments of the source code it does say:
This attribute provides server-side url validation equivalent to jquery validate, and therefore shares the same regular expression. See unit tests for examples.
I need to implement a method which extracts a hostname from FQDN. For example if a hypothetical mail server is mymail.somecollege.edu I want to get as a result mymail
And if I get illegal string (not real FQDN) need no get null or some error code
How can I extract hostname?-
I don`t want to make a parsing of the input by myself.But rather looking for existing API.
Thanks
I tried to search for the first dot '.' - substring before it is a hostname.
But I am looking for existing API
I could not find any helper/API class to obtain the hostname (mymail) from the FQDN (mymail.somecollege.edu). You may have to just parse it like you mentioned: Extract everything up to the first "." character. NOTE: hostnames are not allowed to contain "." character.
var fullyQualifiedDomainName = Dns.GetHostEntry("computer").HostName;
var hostName = fileComputerName.Substring(0, fullyQualifiedDomainName
.IndexOf("."));
I want to validate a URL using regular expression. Following are my conditions to validate the URL:
Scheme is optional
Subdomains should be allowed
Port number should be allowed
Path should be allowed.
I was trying the following pattern:
((http|https)://)?([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?
But I am not getting the desired results. Even an invalid URL like '*.example.com' is getting matched.
What is wrong with it?
are you matching the entire string? you don't say what language you are using, but in python it looks like you may be using search instead of match.
one way to fix this is to start you regexp with ^ and end it with $.
While parsing URL's is best left to a library (since I know perl best, I would suggest something like http://search.cpan.org/dist/URI/), if you want some help debugging that statement, it might be best to try it in a debugger, something like: http://www.debuggex.com/.
I think one of the main reasons it is matching, is because you don't use beginning and ending string match markers. Meaning, no part of that string might be matching what you put in explicitly, but because you haven't marked it with beginning and end markers for the string, your regex could just be matching 'example.com' in your string, not the entire input.
Found the regular expression for my condition with help from your inputs
^(http(s)?://)?[0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*(:(0-9)*)*(\/?)([a-zA-Z0-9\-\.\?\,\'\/\\\+&%\$#_]*)?$
Following code works for me in c#
private static bool IsValidUrl(string url)
{
return new Regex(#"^(http|http(s)?://)?([\w-]+\.)+[\w-]+[.\w]+(\[\?%&=]*)?").IsMatch(url) &&!new Regex(#"[^a-zA-Z0-9]+$").IsMatch(url);
}
it allows "something.anything (at least 2 later after period) with or without http(s) and www.
This question already has answers here:
How to check whether a string is a valid HTTP URL?
(11 answers)
Closed 8 years ago.
I am trying to filter out invalid url from valid ones using .NET.
I am using Uri.TryCreate() method for this.
It has the following syntax
public static bool TryCreate(Uri baseUri,string relativeUri,out Uri result)
Now I am doing this....
Uri uri = null;
var domainList = new List<string>();
domainList.Add("asas");
domainList.Add("www.stackoverflow.com");
domainList.Add("www.codera.org");
domainList.Add("www.joker.testtest");
domainList.Add("about.me");
domainList.Add("www.ma.tt");
var correctList = new List<string>();
foreach (var item in domainList)
{
if(Uri.TryCreate(item, UriKind.RelativeOrAbsolute, out uri))
{
correctList.Add(item);
}
}
I am trying the above code I expect it to remove asas and www.joker.testtest from the list, but it doesnt.
Can some one help me out on this.
Update :
just tried out with Uri.IsWellFormedUriString this too did'nt help.
More Update
List of Valid uri
http://www.ggogle.com
www.abc.com
www.aa.org
www.aas.co
www.hhh.net
www.ma.tt
List of invalid uri
asas
as##SAd
this.not.valid
www.asa.toptoptop
You seem to be confused about what exactly URL (or URI, the difference is not significant here) is. For example, http://stackoverflow.com is a valid absolute URL. On the other hand, stackoverflow.com is technically a valid relative URL, but it would refer to the file named stackoverflow.com in the current directory, not the website with that name. But stackoverflow.com is a registered domain name.
If you want to check whether a domain name is valid, you need to define what exactly do you mean by “valid”:
Is it a valid domain name? Check whether the string consists of parts separated by dots, each part can contain letters, numbers and a hyphen (-). For example, asas and this.not.valid are both valid domain names.
Could it be an Internet domain name? Domain names on the Internet (as opposed to intranet) are specific in that they always have a TLD (top-level domain). So, asas certainly isn't an Internet domain name, but this.not.valid could be.
Is it a domain name under existing TLD? You can download the list of all TLDs and check against that. For example, this.not.valid wouldn't be considered valid under this rule, but thisisnotvalid.com would.
Is it a registered domain name?
Does the domain name resolve to an IP address? A domain name could be registered, but it still may not have an IP address in its DNS record.
Does the computer the domain name points to respond to requests? The requests that make the most sense are a simple HTTP request (e.g. trying to access http://domaininquestion/) or ping.
Try this one:
public static bool IsWellFormedUriString(
string uriString,
UriKind uriKind
)
Or Alternativly you can do this using RegExp like :
^http\://[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(/\S*)?$
Take alook at this list
The problem is that none of the urls you have added here will classify as Absolute URLs. For that you have to prefix the protocol of the URL to it.
You can test and find out that
www.stackoverflow.com - Relative URL
http://www.stackoverflow.com - Absolute URL
//www.stackoverflow.com - Absolute URL ( No surprise here. Refer RFC 3986: "Uniform Resource Identifier (URI): Generic Syntax", Section 4.2 )
The point is that you have to prefix at least // to show that its an absolute URL.
So, in a nutshell, since all your URLs are relative URLs, it passes all your tests.
All your examples are valid,
some are absolute URLs some are relative, thats why none are getting removed.
Else for each Uri, you might try and construct a HttpWebRequest class
and then check for correct responses.
After checking other's answer I am aware that you are not looking for existence of domain and ping back you need to test them based on your GRAMMER... or Syntax of domain name right?
For that you need to rely on regex test only... and make proper rule to eveluate the domain name and if they fail exclude them from the list.
You can adopt these patterns and modify one to suite your need and then test them with every element in the list.
all of your URIs are Well-Formatted URIs so TryCreate and IsWellFormedUriString will not work in your case.
from here, the solutions is trying to open the URI:
using(var client = new MyClient()) {
client.HeadOnly = true;
// fine, no content downloaded
string s1 = client.DownloadString("www.stackoverflow.com");
// throws 404
string s2 = client.DownloadString("www.joker.testtest");
}
This question already has answers here:
What is the best regular expression to check if a string is a valid URL?
(62 answers)
Closed 8 years ago.
I use Asp.net 4 and C# Web Forms.
In my Web Application Users can add URLS using a TextBox.
I need to make sure that every value inserted has a syntactically correct URL format (I do not need to check if the URL really exists).
So as first rule I would like check using a CustomValidator Control if the Input inserted by the User has the value string "http://" at the beginning.
My questions?
Are you able to provide me a RegEx to add to my CustomValidator Control that will let to pass only string beginning with "http://"?
Do you have any other rule using RegEx to suggest me?
What is you best practice to detect detect syntactically correct URL?
Thanks for your help
An easier approach in many ways, and more flexible to later changes, is to just try it and see:
public static bool IsValidHttpUri(string uriString)
{
Uri test = null;
return Uri.TryCreate(uriString, UriKind.Absolute, out test) && test.Scheme == "http";
)
Using Uri.IsWellFormedUriString is easier still, but doesn't check your requirement that the URI must be an HTTP one.
Edit: Oh, whether this considers IRIs valid or not depends on configuration, see the section on "International Resource Identifier Support" at http://msdn.microsoft.com/en-us/library/system.uri.aspx As a rule, whether you want them to be considered valid or not will match this configuration setting anyway, so this is actually a benefit in most cases.
Regex regx = new Regex("http://([\\w+?\\.\\w+])+([a-zA-Z0-9\\~\\!\\#\\#\\$\\%\\^\\&\\*\\(\\)_\\-\\=\\+\\\\\\/\\?\\.\\:\\;\\'\\,]*)?", RegexOptions.IgnoreCase);
In my (limited) experience regex wastes a lot of resources for such a simple task (string starts with http:// or https://)
You might want to consider checking if the url has 'illegal' characters.
about urlencoding