C# 从libsyn下载pdf和mp3

C# 从libsyn下载pdf和mp3,c#,webclient,C#,Webclient,主要故事: static string _sourcePdf = "http://media.libsyn.com/talktomeinkorean/ttmik-l{0}l{1}.{2}"; public static void Download(Level level, int lesson, FileType fileType, string path) { if (level == Level.One && lesson > 25) {

主要故事:

static string _sourcePdf = "http://media.libsyn.com/talktomeinkorean/ttmik-l{0}l{1}.{2}";

public static void Download(Level level, int lesson, FileType fileType, string path)
{
    if (level == Level.One && lesson > 25)
    {
        return;
    }

    WebClient client = new WebClient();
    Uri _uri = new Uri(string.Format(_sourcePdf, (int)level, lesson, fileType));
    string _fileName = string.Format("{0}Level{1}-Lesson{2}.{3}", path, (int)level, lesson, fileType);
    client.DownloadFile(_uri, _fileName);
}
我妹妹想学习韩语,她要求在上述网站上下载
PDF
MP3
文件,我认为这只是一个音频文件和PDF文件,所以我接受了她的要求,所以我访问了网站,然后我意识到她想要的是整个电子书和有声书,因此,我决定制作一个程序自动下载文件,而不是手动下载


我的目标是从libsyn下载
PDF
MP3
文件,但问题是永久链接重定向到新生成的临时链接,并且无法通过
WebClient
访问

例如:

static string _sourcePdf = "http://media.libsyn.com/talktomeinkorean/ttmik-l{0}l{1}.{2}";

public static void Download(Level level, int lesson, FileType fileType, string path)
{
    if (level == Level.One && lesson > 25)
    {
        return;
    }

    WebClient client = new WebClient();
    Uri _uri = new Uri(string.Format(_sourcePdf, (int)level, lesson, fileType));
    string _fileName = string.Format("{0}Level{1}-Lesson{2}.{3}", path, (int)level, lesson, fileType);
    client.DownloadFile(_uri, _fileName);
}
如果我尝试访问permanet链接

此链接将自动重定向到临时链接

并显示PDF或MP3文件

代码片段:

static string _sourcePdf = "http://media.libsyn.com/talktomeinkorean/ttmik-l{0}l{1}.{2}";

public static void Download(Level level, int lesson, FileType fileType, string path)
{
    if (level == Level.One && lesson > 25)
    {
        return;
    }

    WebClient client = new WebClient();
    Uri _uri = new Uri(string.Format(_sourcePdf, (int)level, lesson, fileType));
    string _fileName = string.Format("{0}Level{1}-Lesson{2}.{3}", path, (int)level, lesson, fileType);
    client.DownloadFile(_uri, _fileName);
}

所以我的想法是首先导航到permanet链接,在重定向到临时链接后,我将检索临时链接并设置为新的URI进行下载-“你知道如何在
WebClient
中实现这一点吗?”

对于我通常使用的工作。您可以用C#或任何其他方式生成命令字符串,只需让wget完成以下重定向或重新连接的所有脏活


在命令提示符中粘贴命令序列,或通过
Process.Start()运行命令序列

如果您使用
HttpWebRequest
默认情况下,它会遵循任何重定向()。

可能重复@azt,你在开玩笑吗?哈哈。可以从Process.Start(“浏览器”)获取重定向的uri吗?不,我的意思是,如果你的系统上没有wget,请安装wget。然后只需运行一个
wget“http://www.whatever.com/file.pdf“
然后您就可以下载文件了。大多数时候,我只是在文本编辑器中打开html页面,通过regex提取URL,将
wget
放在前面,并将其粘贴到命令提示符中,然后下载所有文件。比用C语言编写程序快得多。除了需要解析子URL的更复杂工作之外。对于Level1第1课mp3:
wget-O TTMIK-Lesson-L1L1.mp3http://media.libsyn.com/media/talktomeinkorean/TTMIK-Lesson-L1L1.mp3
。对我有用。以
-O
指定的名称将其下载到当前目录实际上不需要C::如果您在Windows上,只需使用
for/L%a IN(1,1,9)DO for/L%b IN(1,1,30)DO wget-O TTMIK-Lesson-L%aL%b.mp3http://media.libsyn.com/media/talktomeinkorean/TTMIK-Lesson-L%aL%b.mp3
。但是,如果你真的喜欢这些资源,考虑购买他们提供的批量下载。