简体   繁体   English

从重定向C#的链接下载文件

[英]Download file from a link that redirects c#

I'm quite new to C# and I need to download a file from this link: http://www.addic7ed.com/original/94097/1 the problem is that it keeps redirecting me to the page I got the url from. 我是C#的新手,我需要从以下链接下载文件: http : //www.addic7ed.com/original/94097/1问题是它一直将我重定向到我从其获得URL的页面。 ( original url ) 原始网址

Couldn't manage to do it with WebClient neither with HttpWebRequest, but maybe its me doing something wrong. 无法使用WebClient或HttpWebRequest都无法做到这一点,但是也许我做错了什么。 Any hints on how I could do this? 关于如何执行此操作的任何提示?

even tried this 甚至尝试

edit: to be more specific, the url was supposed to download a .srt file and so it does when I click it on chrome. 编辑:更具体地说,URL应该下载一个.srt文件,所以当我在chrome上单击它时也是如此。 However, when I try to download the .srt file through c# I am being redirected and end up downloading the site's html source. 但是,当我尝试通过c#下载.srt文件时,将被重定向并最终下载该站点的html源。 How can I replicate chrome's behavior? 如何复制Chrome的行为?

edit2: managed to do it by importing to webclient headers from a webbrowser. edit2:通过从Web浏览器导入到webclient标头来做到这一点。 Still interested in other ways to do it, tho. 仍然对其他方式感兴趣。 This one seems durty to me. 这对我来说似乎很愚蠢。

This behaviour is happening on the server side, not the client side - ie, it's not a C# problem. 此行为发生在服务器端,而不是客户端-即,这不是C#问题。 I can get the URL you request fine using curl . 我可以使用curl获得您要求的URL。 Maybe try setting a different user-agent string on the WebClient or HttpWebRequest objects, if you can do that? 也许尝试在WebClientHttpWebRequest对象上设置其他user-agent字符串,如果可以的话?

Looks like the link to the file is different it's: http://ewns-hls-b-cdn.hexaglobe.net/12914be14a5e488d8e028b7e4e0c0eab/55168b69/euronews_eng.smil/chunklist_b750000.m3u8 . 看起来文件的链接是不同的: http : //ewns-hls-b-cdn.hexaglobe.net/12914be14a5e488d8e028b7e4e0c0eab/55168b69/euronews_eng.smil/chunklist_b750000.m3u8 It uses this link somewhere under the hood you can check all the call that browser performs via Fiddler and perform the same steps in c# code 它在引擎盖下的某个地方使用了此链接,您可以检查浏览器通过Fiddler执行的所有调用,并在c#代码中执行相同的步骤

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM