【问题标题】:How to handle URL having no specific suffix如何处理没有特定后缀的 URL
【发布时间】:2014-11-26 05:14:06
【问题描述】:

我的下载器代码使用两个文本框:

  1. 两个获取用户URL

  2. 指定驱动器

     private void downloadbtn_Click(object sender, EventArgs e)
      {
    
        WebClient myWebClient = new WebClient();
    
        //Declarations for string objects
        string downloadURL, path;
        //raw URL taken from user
       downloadURL =  this.downloadURL.Text;
    
       path = savePath.Text;
    
    
    
        Uri tmp = new Uri(downloadURL);
       string EndPathFileName = tmp.Segments.Last();
       path = path + @"\" + EndPathFileName;
    
       //downloads file using async method
    
       myWebClient.DownloadFileAsync(tmp, savePath.Text);
      }
    

当我使用以下类型的 URL 时会出现问题: http://www.dailymotion.com/video/x1coxfe_aryan-khan-tera-pyar-official-music-video-hd_music 没有任何后缀,即 mp3、mp4 等。 与具有 *.mp3/4 等的 url 相比,它在这里下载了一个零数据的图标

大家有什么建议

【问题讨论】:

标签: c#


【解决方案1】:

您应该检查 Content-Type 标头而不是 URL 以了解文件类型。你可以像这样阅读内容类型

string type = client.ResponseHeaders["content-type"];

【讨论】:

  • 即使对于以前对我有用的 URL,它也会返回 NullReferenceException
  • ResponseHeaders是空还是类型变量是空?
  • NullReferenceException 被抛出 WebHeaderCollection myWebHeaderCollection = myWebClient.ResponseHeaders;
  • 所以“myWebClient”由于某种原因为空?
  • “myWebClient”是否为空,如果是,为什么?
猜你喜欢
  • 1970-01-01
  • 2020-06-29
  • 1970-01-01
  • 2013-11-15
  • 2013-09-27
  • 2012-05-17
  • 1970-01-01
  • 1970-01-01
  • 2015-08-24
相关资源
最近更新 更多