避免从具有有效URI的本地资源下载。带有隐式或显式文件路径的URI

问题描述 投票:1回答:1

使用System.Net.WebClient.DownloadFile方法。我可以从指定的URI下载文件。

我的问题是,URI可能会定向到有效的本地文件:

  • “ C:/Directory/FileName.ext”
  • “ ../../ Directory / FileName.ext”

我希望这些文件路径为无效的URI或强制WebClient仅访问Http / Ftp。

foreach(var link in inputs){
    Download(link)
}

void Download(string source)
{
    var uri = new URI(source);

    //Stop me from downloading from local file

    var response = webClient.DownloadData(uri); 
}
c# download uri
1个回答
0
投票

您可以看一下SchemeUri属性。

它:

  • 是uri的字符串http,其路径类似于http://www.test.com/index.html
  • 是uri的字符串file,其路径类似于c:\test.txt
  • 将[uri]扔给InvalidOperationException

借助于IsAbsoluteUri属性,我认为可以这样测试有效的uri:

var scheme = uri.IsAbsoluteUri ? uri.Scheme : null; // can be put in a SchemeOrNull extension method
if (acceptedSchemes.Contains(scheme))  // acceptedSchemes contains "http", "ftp", ..
{
  ...
© www.soinside.com 2019 - 2024. All rights reserved.