代码之家  ›  专栏  ›  技术社区  ›  Nick Haslam

下载一个文件,我得到的内容长度头是无效的

  •  0
  • Nick Haslam  · 技术社区  · 15 年前

    我正在使用以下代码从Web服务器下载文件,并收到以下错误:

    从保存文件时出错URL:服务器违反了协议。

    Section=ResponseHeader Detail='Content-Length'标头值无效

    从运行小提琴手的同时,它说:

    代码:

    public static bool SaveFileFromURL(string url, string destinationFileName, int timeoutInSeconds)
            {
                //SetAllowUnsafeHeaderParsing20();
                Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                SettingsSection section = (SettingsSection)config.GetSection("system.net/settings");
                section.HttpWebRequest.UseUnsafeHeaderParsing = false;
                config.Save();
    
                // Create a web request to the URL
                HttpWebRequest MyRequest = (HttpWebRequest)WebRequest.Create(url);
                MyRequest.UseDefaultCredentials = true;
                MyRequest.ContentLength = 0;
    
                MyRequest.Timeout = timeoutInSeconds * 1000;
                try
                {
                    // Get the web response
                    HttpWebResponse MyResponse = (HttpWebResponse)MyRequest.GetResponse();
    
                    // Make sure the response is valid
                    if (HttpStatusCode.OK == MyResponse.StatusCode)
                    {
                        // Open the response stream
                        using (Stream MyResponseStream = MyResponse.GetResponseStream())
                        {
                            // Open the destination file
                            using (FileStream MyFileStream = new FileStream(destinationFileName, FileMode.OpenOrCreate, FileAccess.Write))
                            {
                                // Create a 4K buffer to chunk the file
                                byte[] MyBuffer = new byte[4096];
                                int BytesRead;
                                // Read the chunk of the web response into the buffer
                                while (0 < (BytesRead = MyResponseStream.Read(MyBuffer, 0, MyBuffer.Length)))
                                {
                                    // Write the chunk from the buffer to the file
                                    MyFileStream.Write(MyBuffer, 0, BytesRead);
                                }
                            }
                        }
                    }
                }
                catch (Exception err)
                {
                    throw new Exception("Error saving file from URL:" + err.Message, err);
                }
                return true;
            }
    

    更新 :如果我将URL直接传递到浏览器中,则文件将成功下载,并在GetResponse行引发错误。

    更新2 :我得到相同的错误WebClient下载文件:

    public static bool DL_Webclient(string url, string destinationFileName)
    {
        WebClient myWebClient = new WebClient();
        myWebClient.UseDefaultCredentials = true;
        myWebClient.DownloadFile(url, destinationFileName);
    
        return true;
    }
    

    :在检索到邮件中的其他标题(使用Fiddler)后,它们是:

    HTTP/1.1 200 OK
    Connection: close
    Date: Wed, 03 Mar 2010 08:43:06 GMT
    Server: Microsoft-IIS/6.0
    X-Powered-By: ASP.NET
    Content-Length: 13314320
    Content-Type: application/x-evsaveset
    Set-Cookie: ASPSESSIONIDQQCQSCRC=CFHHJHADOIBCFAFOHFJCDNEG; path=/
    Cache-control: private
    
    2 回复  |  直到 15 年前
        1
  •  0
  •   Marvin Smit    15 年前

    是否存在其他HTTP头?

    W3C website More W3C website

        2
  •  0
  •   PaulB    15 年前