0

以下のコードを使用して Web サーバーからファイルをダウンロードしていますが、次のエラーが表示されます。

エラー: URL からファイルを保存中にエラーが発生しました: サーバーがプロトコル違反を犯しました。

Section=ResponseHeader Detail='Content-Length' ヘッダー値が無効です

これが実行されている間に Fiddler を実行すると、次のように表示されます。

Content-Length 応答ヘッダーは有効な符号なし整数ではありません 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
4

2 に答える 2

0

代わりにWebClient.DownloadFileを使用できませんか?

于 2010-03-03T08:32:16.167 に答える
0

他の HTTP ヘッダーは存在しますか?

「Transfer-Encoding」ヘッダーまたは同様のルールに関係している可能性があります。これらのヘッダーと Content-Length ヘッダーへの影響の詳細については、W3C Web サイトおよびMore W3C Web サイトを参照してください。

お役に立てれば、

于 2010-03-02T20:49:46.673 に答える