Esempio n. 1
0
        public static string TryParseAndFixWebAddress(string webAddress)
        {
            if (!Uri.IsWellFormedUriString(webAddress, UriKind.RelativeOrAbsolute))
            {
                if (!Regex.IsMatch(webAddress, Constants.TextAnalysisConfiguration.BadOcrWebAddressRegex))
                {
                    return(string.Empty);
                }

                for (int i = 2; i < 4; i++)
                {
                    var webAddressTopLevelDomain = new string(
                        webAddress
                        .Reverse()
                        .Take(i)
                        .Reverse()
                        .ToArray()
                        );

                    if (FileReader.GetFileLines(Constants.TopLevelDomainListPath).Contains(webAddressTopLevelDomain.ToUpper()))
                    {
                        webAddress = webAddress.Insert(webAddress.IndexOf(webAddressTopLevelDomain), ".");
                        break;
                    }
                }
            }

            if (Regex.IsMatch(webAddress, Constants.TextAnalysisConfiguration.WebAddressRegex))
            {
                try
                {
                    var webAddressUri = new UriBuilder(webAddress).Uri;
                    if (Constants.ValidateWebAddresses)
                    {
                        return(HttpClientHelpers.IsWebsiteAvailable(webAddressUri)
              ? webAddress
              : string.Empty);
                    }

                    return(webAddressUri.AbsoluteUri);
                }
                catch (UriFormatException)
                {
                    return(string.Empty);
                }
            }

            return(string.Empty);
        }