C# 如何检索存储在 Url 协议中的超链接字符串?

C# How to retrieve a hyperlink string stored in a Url Protcool?

简介:

我遇到了设计瓶颈,遇到了一个我无法解决的问题。我可以使用指导。

我正在创建一个应用程序,我想将 url protocols 用于 accessibility/security 和用户便利。直到我准备好构建 API。到目前为止,我已经将我想要的功能编码到我的应用程序中,包括下载方法和标准 url protocool 程序。

问题:

我使用的所有下载地址都经过编码,解析后需要WebUtility.UrlDecode()才能提取可用的下载地址。 问题是:如何存储左键单击生成的地址并将其传递给我的file downloader方法?我对如何完成这项任务感到困惑。 重申:我想通过左键单击捕获我的 url protocol 在线托管的一部分,并将其存储在一个变量中以供使用。

程序应该如何 运行:

注意: ✔️ = 工作。 ❌ = 不工作。

  1. 一位未知用户在“页面”上,并向其提供了一个选择 Download & Install via through App ✔️
  2. 用户左键单击 hyperlink。 ✔️
  3. 下载地址信息传递给应用程序。 ❌
  4. 应用程序打开。 ✔️
  5. 下载启动“x-item”并安装(仅当我手动插入 link 时有效)。 ✔️

实际地址示例:

<a href="myApp://https%4A%7F%0B.amazon.8943fj9f8j3%2Ftest.json" target="_blank">Download & Install via myApp</a>

我想存储...

https%4A%7F%0B.amazon.8943fj9f8j3%2Ftest.json //store this. 

//and it would be decoded and made into a usable address to download from...
Example code...
//output desired from the original url. 
string arg = "https%4A%7F%0B.amazon.8943fj9f8j3%2Ftest.json ";
string encoded_url = arg.Trim().Split('/').Last();
string url = Uri.UnescapeDataString(enc_url);
//output example --> https://www.example.com/yadayada/sample.json

TLDR: 为了简单明了……在我看来,该应用程序的工作方式类似于用户加入不和谐服务器的方式。用户可以点击 discord server invite-hyperlink,然后 discord 应用程序会打开一条提示消息,询问用户是否想加入服务器。

基本代码。

        static void Main()

            //TESTING...START

            string[] args = Environment.GetCommandLineArgs();

            //args[0] is always the path to the application
            RegisterMyAppProtocol(args[0]);
            //the above method posted before, that edits registry      

            try
            {
                Console.WriteLine("Argument: " + args[1].Replace("myapp:", string.Empty));
            }
            catch
            {
                Console.WriteLine("No argument(s)");  //if there's an exception, there's no argument
            }

            Console.ReadLine();

            //TESTING...END


        }

        static void RegisterMyAppProtocol(string AppPath)  //myAppPath = full path to application
        {
            //open myApp protocol's subkey
            RegistryKey key = Registry.ClassesRoot.OpenSubKey("myApp");


            //if the protocol is not registered yet...register it
            if (key == null)  
            {
                key = Registry.ClassesRoot.CreateSubKey("myApp");
                key.SetValue(string.Empty, "URL: myApp Protocol");
                key.SetValue("URL Protocol", string.Empty);

                key = key.CreateSubKey(@"shell\open\command");
                key.SetValue(string.Empty, AppPath.Replace("dll", "exe") + " " + "%1");
                //%1 represents the argument - this tells windows to open this program with an argument / parameter
            }

            key.Close();
        }

我链接了多个参数并且能够找到我的问题的解决方案。 结果是 rawUrl/decodedUrl 变量 return 作为按协议 url 超链接所需的字符串。

            var rawUrl = string.Empty;
            var decodedUrl = string.Empty;

            try
            {
                //if there's an argument passed, write it
                rawUrl = args[0];
                if (rawUrl[rawUrl.Length - 1] == '/')
                    rawUrl = rawUrl.Remove(rawUrl.Length - 1);
                Console.WriteLine($"Argument: {rawUrl}");
                decodedUrl = returnURL(rawUrl);
            }
            catch
            {
                Console.WriteLine("No argument(s)");  //if there's an exception, there's no argument
            }


            if (!string.IsNullOrEmpty(decodedUrl))
            {
                  //input method details so it does stuff. 
            }