Friday, July 19, 2019

How to expand URLs in C-Sharp?





 How to expand URLs in C#?



  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
using System; using System.Text.RegularExpressions; using System.Net;  
using System.Web;
     
public static class Program
{
 public static string URLExpander(this string URL) {
 
      //removal ASCII and UNICODE control characters, if they exist
            string urlText = Regex.Replace(URL, @"[\u0000-\u0008|\u000B-\u000C|\u000E-\u001F|\u0080-\u009F]", "");

            if (string.IsNullOrWhiteSpace(urlText))
                return string.Empty; 

            Uri uriResult;

      //validate URL
            bool result = Uri.TryCreate(urlText, UriKind.Absolute, out uriResult)
            && (uriResult.Scheme == Uri.UriSchemeHttp || uriResult.Scheme == Uri.UriSchemeHttps
    || uriResult.Scheme == Uri.UriSchemeFtp || uriResult.Scheme == Uri.UriSchemeFile
       ); //or other schemes

            if (!result)
            return string.Empty;

            //https://docs.microsoft.com/en-us/dotnet/api/system.net.httpwebrequest?view=netframework-4.8
      HttpWebRequest req = null; 
            HttpWebResponse response; 
  
            try
            {
             req = (HttpWebRequest)WebRequest.Create(uriResult); 
                req.Method = "HEAD";
                req.AllowAutoRedirect = false;
                result = true; 
            }
            catch (NotSupportedException)
            {
                result = false; //add your exception handling here
                
            }
            catch (ArgumentNullException)
            {
                result = false; //add your exception handling here

            }
            catch (System.Security.SecurityException)
            {
                result = false; //add your exception handling here

            }
            catch (UriFormatException)
            {
                result = false; //add your exception handling here
   }

            if (!result)
             return string.Empty; 
            
   string redirectedURL = ""; 
            try
            {
                response =  (HttpWebResponse)req.GetResponse();
    
    //https://en.wikipedia.org/wiki/List_of_HTTP_status_codes
    //304 Not Modified - Redirection to a previously cached result -> may not be a URL redirection
    //restrictive
    if ((int)response.StatusCode >= 300 && ((int)response.StatusCode < 400))
    {
     redirectedURL = response.Headers[HttpResponseHeader.Location]; //or url = response.Headers["Location"];
    }
    else if ((int)response.StatusCode < 300 || (int)response.StatusCode >= 400) //okay
    {
     redirectedURL = uriResult.ToString(); //set as validated URL
    }        

    result = true; 
            }       
            catch (System.Net.WebException)
            {
                result = false; 
            }
            catch (System.Net.ProtocolViolationException)
            {
                result = false; 
            }
            catch (InvalidOperationException)
            {
                result = false;                 
            }
            catch (NotSupportedException)
            {
                result = false; 
            }
            
            if (!result)
             return string.Empty;
  
   try {
             
    redirectedURL = HttpUtility.UrlDecode(redirectedURL);
   }
   catch 
            {
                return string.Empty; 
            }
  
      return redirectedURL; 
  
 }
 
 public static void Main()
 {
  string testURL = "http://bit.ly/AzureAITranslation"; string testURL2 = "https://metadataconsulting.blogspot.com"; 
  
  //sanity check 
  //if (testURL == testURL.URLExpander()) //no redirect especially for status code 304!
     
  Console.WriteLine(testURL + "\nexpands to\n" + testURL.URLExpander());
  Console.WriteLine(); 
  Console.WriteLine(testURL2 + "\nexpands to\n" + testURL2.URLExpander());
 }
}


No comments:

Post a Comment