Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace HTTPProtocol_exercises___Validate_URL
- {
- using System;
- using System.Net;
- using System.Text.RegularExpressions;
- class Program
- {
- static void Main()
- {
- string input = WebUtility.UrlDecode(Console.ReadLine());
- Regex regexCheck = new Regex(@"(https?):\/{2}([a-zA-Z0-9\.-]+)(\:\d+)?([\/][\w\W]+)");
- Match match = regexCheck.Match(input);
- string protocol = match.Groups[1].Value;
- int defaultPort = protocol == "http" ? 80 : 443;
- string host = match.Groups[2].Value;
- string port = match.Groups[3].Value ?? defaultPort;
- string path = match.Groups[4].Value;
- string queryStrings = match.Groups[5].Value;
- string fragment = match.Groups[6].Value;
- if (string.IsNullOrEmpty(protocol))
- {
- Console.WriteLine("Invalid URL");
- return;
- }
- if (string.IsNullOrEmpty(host))
- {
- Console.WriteLine("Invalid URL");
- return;
- }
- if (string.IsNullOrEmpty(path))
- {
- Console.WriteLine("Invalid URL");
- return;
- }
- Console.WriteLine("Protocol: {0} \nHost: {1} \nPort: {2} \nPath: {3}", protocol, host, port, path);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement