简体   繁体   English

从URL下载文件,在C#中没有扩展名

[英]Downloading file from URL with no extension in C#

I'm trying to get the zip file out of this link with C#: http://dl.opensubtitles.org/en/download/sub/4860863 我想用C#从这个链接中获取zip文件: http//dl.opensubtitles.org/en/download/sub/4860863

I've tried: string ResponseText; 我试过了:string ResponseText;

        HttpWebRequest m = (HttpWebRequest)WebRequest.Create(o.link);
        m.Method = WebRequestMethods.Http.Get;

        using (HttpWebResponse response = (HttpWebResponse)m.GetResponse())
        {

            using (StreamReader reader = new StreamReader(response.GetResponseStream()))
            {

               ResponseText = reader.ReadToEnd();

                // ResponseText = HttpUtility.HtmlDecode(ResponseText);
                XmlTextReader xmlr = new XmlTextReader(new StringReader(ResponseText));


            }
        }

and

  WebRequest request = WebRequest.Create(o.link);
        using (WebResponse response = request.GetResponse())
        using (Stream stream = response.GetResponseStream())
        {

            string contentType = response.ContentType;
            // TODO: examine the content type and decide how to name your file
            string filename = "test.zip";

            // Download the file
            using (Stream file = File.OpenWrite(filename))
            {
                // Remark: if the file is very big read it in chunks
                // to avoid loading it into memory
                byte[] buffer = new byte[response.ContentLength];
                stream.Read(buffer, 0, buffer.Length);
                file.Write(buffer, 0, buffer.Length);
            }
        }

But they all return something weird, nothing that looks like the file I need... I think the link is php generated, but I'm not sure... The opensubtitles api is no option for me... Many thanks 但是它们都返回了一些奇怪的东西,没有看起来像我需要的文件......我认为链接是php生成的,但我不确定... opensubtitles api对我来说是没有选择...非常感谢

It seems the Content-Type response is ok for me for your link: 对于我的链接,似乎Content-Type响应是可以的:

Request URL:http://dl.opensubtitles.org/en/download/sub/4860863
Request Method:GET
Status Code:200 OK
Request Headersview:
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*//*;q=0.8
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Cookie:PHPSESSID=gk86hdrce96pu06kuajtue45a6; ts=1372177758
Host:dl.opensubtitles.org
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.116 Safari/537.36
Response Headersview:
Accept-Ranges:bytes
Age:0
Cache-Control:must-revalidate, post-check=0, pre-check=0
Connection:keep-alive
Content-Disposition:attachment; filename="the.dark.knight.(2008).dut.1cd.(4860863).zip"
Content-Length:48473
Content-Transfer-Encoding:Binary
Content-Type:application/zip
Date:Tue, 25 Jun 2013 16:29:45 GMT
Expires:Mon, 1 Apr 2006 01:23:45 GMT
Pragma:public
Set-Cookie:ts=1372177785; expires=Thu, 25-Jul-2013 16:29:45 GMT; path=/
X-Cache:MISS
X-Cache-Backend:web1

I have check your code and test it using the link and the manual download produced a 48473 bytes file, and using your code produced 48564 bytes with zero after 0xDC2 and when I compared it with Hex editor, it have many different part. 我检查了你的代码并使用链接进行测试,手动下载产生了一个48473字节的文件,并使用你的代码在0xDC2之后生成了48564字节和零,当我将它与Hex编辑器进行比较时,它有许多不同的部分。 We may need to put more request header before sending the request. 我们可能需要在发送请求之前添加更多请求标头。

ok, now i can resolve it: put cookie and read at a smaller chunk 好的,现在我可以解决它:把cookie放在一个较小的块上

private void button1_Click(object sender, EventArgs e) {
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri("http://dl.opensubtitles.org/en/download/sub/4860863"));
    //request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.116 Safari/537.36";
    //request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*//*;q=0.8";
    //request.Headers["Accept-Encoding"] = "gzip,deflate,sdch";
    request.Headers["Cookie"] = "PHPSESSID=gk86hdrce96pu06kuajtue45a6; ts=1372177758";
    using (WebResponse response = request.GetResponse())
    using (Stream stream = response.GetResponseStream()) {

        string contentType = response.ContentType;
        // TODO: examine the content type and decide how to name your file
        string filename = "test.zip";

        // Download the file
        using (Stream file = File.OpenWrite(filename)) {
            byte[] buffer = ReadFully(stream, 256);
            stream.Read(buffer, 0, buffer.Length);
            file.Write(buffer, 0, buffer.Length);
        }
    }
}

/// <summary>
/// Reads data from a stream until the end is reached. The
/// data is returned as a byte array. An IOException is
/// thrown if any of the underlying IO calls fail.
/// </summary>
/// <param name="stream">The stream to read data from</param>
/// <param name="initialLength">The initial buffer length</param>
public static byte[] ReadFully(Stream stream, int initialLength) {
    // If we've been passed an unhelpful initial length, just
    // use 32K.
    if (initialLength < 1) {
        initialLength = 32768;
    }


    byte[] buffer = new byte[initialLength];
    int read = 0;


    int chunk;
    while ((chunk = stream.Read(buffer, read, buffer.Length - read)) > 0) {
        read += chunk;


        // If we've reached the end of our buffer, check to see if there's
        // any more information
        if (read == buffer.Length) {
            int nextByte = stream.ReadByte();


            // End of stream? If so, we're done
            if (nextByte == -1) {
                return buffer;
            }


            // Nope. Resize the buffer, put in the byte we've just
            // read, and continue
            byte[] newBuffer = new byte[buffer.Length * 2];
            Array.Copy(buffer, newBuffer, buffer.Length);
            newBuffer[read] = (byte)nextByte;
            buffer = newBuffer;
            read++;
        }
    }
    // Buffer is now too big. Shrink it.
    byte[] ret = new byte[read];
    Array.Copy(buffer, ret, read);
    return ret;
}

EDIT: You don't need to set Cookie at all, you'll produce a different file but a valid one. 编辑:您根本不需要设置Cookie,您将生成一个不同的文件但是有效的文件。 I assume the server add extra info to the file when you revisit them. 我假设服务器在您重新访问时向该文件添加额外信息。

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

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