【发布时间】:2016-09-18 17:25:07
【问题描述】:
我有一个 HTTP 协议的下载方法。但它似乎无法正常工作,出了点问题。我用一些 url 源对其进行了测试,除了最后一个之外它是正确的。 url 的 ContentLength 属性错误。它在运行时显示为 210 kb,但实际上是 8 MB。我将通过分享我的代码来展示它。如何解决?
代码:
void TestMethod(string fileUrl)
{
HttpWebRequest req = WebRequest.Create(fileUrl) as HttpWebRequest;
HttpWebResponse resp = req.GetResponse() as HttpWebResponse;
long contentSize = resp.ContentLength;
MessageBox.Show(contentSize.ToString());
}
private void TestButton_Click(object sender, EventArgs e)
{
string url1 = "http://www.calprog.com/Sounds/NealMorseDowney_audiosample.mp3";
string url2 = "http://www.stephaniequinn.com/Music/Canon.mp3";
TestMethod(url1); //This file size must be 8 MB, but it shows up as 210 kb. This is the problem
TestMethod(url2); //This file size is correct here, about 2.1 MB
}
【问题讨论】:
标签: c# http download content-length