Advertisement
Guest User

Untitled

a guest
Nov 19th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.38 KB | None | 0 0
  1. namespace HTTPProtocol_exercises___Validate_URL
  2. {
  3.     using System;
  4.     using System.Net;
  5.     using System.Text.RegularExpressions;
  6.  
  7.     class Program
  8.     {
  9.         static void Main()
  10.         {
  11.             string input = WebUtility.UrlDecode(Console.ReadLine());
  12.             Regex regexCheck = new Regex(@"(https?):\/{2}([a-zA-Z0-9\.-]+)(\:\d+)?([\/][\w\W]+)");
  13.             Match match = regexCheck.Match(input);
  14.             string protocol = match.Groups[1].Value;
  15.             int defaultPort = protocol == "http" ? 80 : 443;
  16.             string host = match.Groups[2].Value;
  17.             string port = match.Groups[3].Value ?? defaultPort;
  18.             string path = match.Groups[4].Value;
  19.             string queryStrings = match.Groups[5].Value;
  20.             string fragment = match.Groups[6].Value;
  21.  
  22.             if (string.IsNullOrEmpty(protocol))
  23.             {
  24.                 Console.WriteLine("Invalid URL");
  25.                 return;
  26.             }
  27.  
  28.             if (string.IsNullOrEmpty(host))
  29.             {
  30.                 Console.WriteLine("Invalid URL");
  31.                 return;
  32.             }
  33.  
  34.             if (string.IsNullOrEmpty(path))
  35.             {
  36.                 Console.WriteLine("Invalid URL");
  37.                 return;
  38.             }
  39.  
  40.             Console.WriteLine("Protocol: {0} \nHost: {1} \nPort: {2} \nPath: {3}", protocol, host, port, path);
  41.  
  42.         }
  43.     }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement