2010-03-02 26 views
0

我下載一個文件,從網絡服務器,使用下面的代碼,和我recieving此錯誤:下載文件,我也得到Content-Length頭是無效

的錯誤:從 保存文件時出錯URL:服務器違反協議。

節= ResponseHeader詳細= 'Content-Length的' 標頭值無效

從而這是運行運行的Fiddler,它說:

內容長度響應標頭是不是一個有效的無符號整數 內容 - 長: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:具有檢索到的其它標題的消息中(使用招),它們是:

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 
+1

你能用瀏覽器下載文件嗎? – 2010-03-02 18:51:36

回答

0

是否還有其他HTTP頭文件存在?

它可能與'Transfer-Encoding'標題或類似的規則有關。有關這些標題及其對Content-Length頭效果更具體的信息可以在W3C websiteMore W3C website

希望這有助於找到,

+0

我已經將其他標題添加到問題(上面) – 2010-03-03 08:47:39

+0

除了我不熟悉的「應用程序/ x-evsaveset」內容類型之外,似乎沒有任何其他標題。這慢慢地開始看起來像HttpRequest類中的錯誤。我知道這些類(.Net 2.0)與REST服務不能很好地協同工作,但這與這裏的AFAICS無關。 你能告訴我那個Content-Type是什麼嗎?我想閱讀那篇文章。也許是一種關係。 – 2010-03-03 08:59:46

0

你能不能用WebClient.DownloadFile呢?

+0

可悲的是,不,我剛剛嘗試過,並且出現相同的錯誤消息,請參閱上面的代碼。不過謝謝... – 2010-03-03 08:42:01