Пример #1
0
        public void GetValid(string url, Action <UrlValidationResponse, Exception> callback)
        {
            try
            {
                var startInfo = new ProcessStartInfo(YoutubeDlFilename)
                {
                    Arguments              = $"--ignore-errors --get-url {url}",
                    CreateNoWindow         = true,
                    UseShellExecute        = false,
                    RedirectStandardOutput = true
                };

                UrlValidationResult validationResult = null;
                using (var ydlProcess = Process.Start(startInfo))
                {
                    if (ydlProcess == null)
                    {
                        callback(null, new Exception("Cannot start validation process."));
                        return;
                    }
                    var output = ydlProcess.StandardOutput;
                    while (!output.EndOfStream)
                    {
                        // Read first line
                        var outputLine = output.ReadLine();
                        validationResult = new UrlValidationResult
                        {
                            IsValid = outputLine.IsValidUrl(),
                            Url     = url
                        };
                        // First line should tell everything, so let's close the process.
                        ydlProcess.Close();
                    }
                }

                if (validationResult == null)
                {
                    validationResult = new UrlValidationResult
                    {
                        IsValid = false,
                        Url     = url
                    }
                }
                ;

                var response = new UrlValidationResponse
                {
                    ValidationResults = new[] { validationResult }
                };
                callback(response, null);
            }
            catch (Exception ex)
            {
                callback(null, ex);
            }
        }
Пример #2
0
        /// <summary>
        /// Checks if an OPC DA server URL is correct.
        /// </summary>
        /// <param name="opcServerUrl">The OPC server URL.</param>
        /// <exception cref="System.ArgumentNullException">opcServerUrl</exception>
        /// <exception cref="UriFormatException">Wrong OPC server URL. Format: opcda://host/progIdOrCLSID.</exception>
        public static void CheckOpcUrl(Uri opcServerUrl)
        {
            UrlValidationResult result = Validate(opcServerUrl);

            if (0 != (result & UrlValidationResult.UrlIsNull))
            {
                throw new ArgumentNullException("opcServerUrl");
            }

            if (0 != (result & UrlValidationResult.MoreThanTwoSegments))
            {
                throw new UriFormatException("Wrong OPC server URL. Format: opcda://host/progIdOrCLSID.");
            }

            if (0 != (result & UrlValidationResult.WrongScheme))
            {
                throw new UriFormatException("Wrong OPC server URL. Format: opcda://host/progIdOrCLSID.");
            }
        }