【问题标题】:Downloading a file, and I get Content-Length header is Invalid下载文件,我得到 Content-Length header is Invalid
【发布时间】:2011-01-22 20:19:36
【问题描述】:

我正在使用以下代码从网络服务器下载文件,但收到此错误:

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

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

在运行时运行 Fiddler,它说:

Content-Length 响应标头不是有效的无符号整数 内容长度:13312583

代码:

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.Downloadfile 相同的错误:

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

    return true;
}

更新 3:检索到消息中的其他标头(使用 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

【问题讨论】:

  • 可以用浏览器下载文件吗?

标签: c# asp.net httpwebrequest httpwebresponse


【解决方案1】:

是否存在其他 HTTP 标头?

这可能与“Transfer-Encoding”标头或类似规则有关。有关这些标头及其对 Content-Length 标头的影响的更多具体信息,请参见 W3C websiteMore W3C website

希望这会有所帮助,

【讨论】:

  • 我已将其他标题添加到问题中(上图)
  • 除了我不熟悉“application/x-evsaveset”的 Content-Type 之外,似乎没有其他标题。这慢慢开始看起来像 HttpRequest 类中的错误。我知道这些类(.Net 2.0)不能很好地与 REST 服务一起使用,但这与 AFAICS 无关。你能告诉我那个 Content-Type 是什么吗?我想读一读。也许有关系。
【解决方案2】:

你能不改用WebClient.DownloadFile吗?

【讨论】:

  • 遗憾的是,不,我刚刚尝试过,同样的错误消息,请参见上面的代码。不过还是谢谢...
猜你喜欢
  • 2020-11-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-15
  • 2021-09-15
  • 1970-01-01
  • 2012-09-08
  • 1970-01-01
相关资源
最近更新 更多