Advertisement
Danny_Berova

HomeworkTestWebProtocol

May 22nd, 2018
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.11 KB | None | 0 0
  1. namespace WebTestsAndDemos
  2. {
  3.     using System;
  4.     using System.Net;
  5.     using System.Text;
  6.  
  7.     class Program
  8.     {
  9.         static void Main()
  10.         {
  11.             //task 1
  12.  
  13.             var inputUriTest = Console.ReadLine();
  14.             var inputUriDecoded = WebUtility.UrlDecode(inputUriTest);
  15.             Console.WriteLine(inputUriDecoded);
  16.  
  17.             //task 2
  18.             try
  19.             {
  20.                 var inputUri = Console.ReadLine();
  21.                 var uriTest = new Uri(inputUri);
  22.  
  23.                 var result = new StringBuilder();
  24.  
  25.                 if (!uriTest.IsWellFormedOriginalString())
  26.                 {
  27.                     result.AppendLine("Invalid URL");
  28.                 }
  29.  
  30.                 var validHttp = uriTest.Port == 80 && uriTest.Scheme == "http";
  31.                 var validHttps = uriTest.Port == 443 && uriTest.Scheme == "https";
  32.  
  33.                 if (validHttps || validHttp)
  34.                 {
  35.                     result.AppendLine($"Protocol: {uriTest.Scheme}");
  36.                     result.AppendLine($"Host: {uriTest.DnsSafeHost}");
  37.                     result.AppendLine($"Port: {uriTest.Port}");
  38.                     var path = WebUtility.UrlDecode(uriTest.LocalPath);
  39.                     result.AppendLine($"Path: {path}");
  40.  
  41.                     var query = WebUtility.UrlDecode(uriTest.Query);
  42.                     if (uriTest.Query != string.Empty)
  43.                     {
  44.                         result.AppendLine($"Query: {query.TrimStart('?')}");
  45.                     }
  46.  
  47.                     var fragment = WebUtility.UrlDecode(uriTest.Fragment);
  48.                     if (uriTest.Fragment != string.Empty)
  49.                     {
  50.                         result.AppendLine($"Fragment: {fragment.TrimStart('#')}");
  51.                     }
  52.                 }
  53.                 else
  54.                 {
  55.                     result.AppendLine("Invalid URL");
  56.                 }
  57.  
  58.                 Console.WriteLine(result.ToString().Trim());
  59.             }
  60.             catch (Exception e)
  61.             {
  62.                 Console.WriteLine("Invalid URL");
  63.             }
  64.         }
  65.     }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement