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



简介:

我遇到了设计上的障碍,遇到了一个我无法解决的问题。我可以寻求指导。

我正在创建一个应用程序,我想使用url protocols来实现可访问性/安全性和用户便利性。直到我准备好构建API。到目前为止,我已经将我想要的功能编码到我的应用程序中,包括下载方法和标准的url protocool过程。

问题:

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

程序应该如何运行:

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

  1. 一个未知的用户正在";一页";并对CCD_ 7进行了选择✔️
  2. 用户左键单击超链接。✔️
  3. 下载地址信息将传递给应用程序。❌
  4. 应用程序打开。✔️
  5. 下载";x项";并安装(仅当我手动插入链接时有效(。✔️

实用地址示例:

<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服务器的方式。用户会点击discord服务器邀请超链接,然后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(@"shellopencommand");
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变量作为按下协议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. 
}

最新更新