2

我正在编写一个 RTSP 客户端模块,为此,我需要解析一个非常可变的 URI。但是我完全不知道应该使用哪种方法(最安全)以及如何实现这一点。

示例 URI 可能如下所示:

rtsp://192.168.1.100:554/videocam/media/1/video/1
\_/    \_______________/\_______/\______________/
 |              |           |           |
scheme      authority     [sub]     [mediacontrol]

但也有其他可能性:

192.168.1.100/videocam/media/1/video/1
192.168.1.100:6000/media/1/video/1
192.168.1.100:6000/videocam

我需要以下信息:

IP         | how can I recognise this pattern [num].[num].[num].[num]?
Port       | easy if the string contains rtsp://, but what about just a number? 1-65555
Sub        | Optional subpath, can completely vary!
MediaLevel | Optional MediaLevel (indicator for stream/track), 
             not to be confused with the path. MediaLevel can be also just like this: track1 or m1s3 or media1/video1.. see?
             I can't go for the slash, also the path also can contain multiple slashes

也许有一个用于此类任务的库?

谢谢你。

4

3 回答 3

3
var uri = new Uri("rtsp://192.168.1.100:554/videocam/media/1/video/1");
var host = uri.Host;
var port = uri.Port;
var sub = uri.Segments[1];
var mlevel = uri.Segments.Skip(2).ToArray();
于 2012-08-20T11:48:15.693 回答
0

这是一个如何使用 UriBuilder 类的快速示例。它有点冗长,因为它是一个示例,还没有准备好投入生产。如果要识别更多的子,则可以将它们添加到子列表中,如示例中所示。

class Program
    {
        private static string _scheme = string.Empty;
        private static string _host = string.Empty;
        private static string _sub = string.Empty;

        static void Main(string[] args)
        {
            ParseUri("rtsp://192.168.1.100:554/videocam/media/1/video/1");
            ParseUri("192.168.1.100/videocam/media/1/video/1");
            ParseUri("192.168.1.100:6000/media/1/video/1");
            ParseUri("192.168.1.100:6000/videocam");
            // example of adding a new sub
            Sub.Add("sample");
            ParseUri("192.168.1.100:6000/sample/");
            Console.ReadLine();
        }

        private static void ParseUri(string URI)
        {
            UriBuilder uri = new UriBuilder(URI);
            _scheme = uri.Scheme;
            _host = uri.Host;
            _sub = string.Empty;
            StringBuilder sb = new StringBuilder();
            foreach (string s in uri.Uri.Segments)
            {
                if (Sub.Contains(s.Replace("/","")))
                {_sub = s;}
                else
                { sb.Append(s); }
            }

            Console.Out.WriteLine("+++++++++++++");
            Console.Out.WriteLine("URI: {0}",URI);
            Console.Out.WriteLine("Scheme: {0}", _scheme);
            Console.Out.WriteLine("sub: {0}",_sub);
            Console.Out.WriteLine("mediaControl: {0}", sb.ToString());
        }

        private static List<string> Sub
        {
            get
            {
                List<string> sub = new List<string>();
                sub.Add("videocam");
                return sub;
            }
        }
    }
于 2012-08-20T13:23:17.710 回答
0
                trace("Url      : {0}", turl.Text);
                var uri = new Uri(turl.Text);
                string host = uri.Host;
                int port = uri.Port;
                string userInfo = uri.UserInfo;
                string subStream = "";
                string userName = "";
                string password = "";
                if (uri.Segments?.Any() == true)
                {
                    subStream = string.Join("", uri.Segments);
                }
                if (!string.IsNullOrEmpty(userInfo))
                {
                    if (userInfo.Contains(":"))
                    {
                        string[] data = userInfo.Split(':');
                        userName = data[0];
                        password = data[1];
                    }
                    else
                    {
                        userName = userInfo;

                    }
                }
                trace("host     : {0}", host);
                trace("port     : {0}", port);
                trace("userName : {0}", userName);
                trace("password : {0}", password);
                trace("subStream: {0}", subStream);

没有用户信息 在此处输入图像描述

于 2020-06-22T15:22:07.553 回答