我尝试download file via WebClient
,但它不是从文件中返回所有字节。Original file has 45Kb
,它download only 8kb
来自总文件。它发生在我添加用户代理标头之后。
我的代码是:
using (ZipFile zip = new ZipFile())
{
foreach (KeyValuePair<string, string> i in filesToInclude)
{
System.Net.WebClient wc = new System.Net.WebClient();
wc.Credentials = System.Net.CredentialCache.DefaultCredentials;
wc.UseDefaultCredentials = true;
wc.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
string downloadUrl = SPContext.Current.Site.Url + i.Key;
AppClass.WriteToLog(string.Format("downloadUrl: {0}", downloadUrl));
byte[] img = wc.DownloadData(downloadUrl);
zip.AddEntry(i.Value, img);
}
zip.Save(Response.OutputStream);
}
你有什么想法吗?
更新:在以前的版本url
中没有正确构建。我将代码更改为:
using (ZipFile zip = new ZipFile())
{
foreach (KeyValuePair<string, string> i in filesToInclude)
{
System.Net.WebClient wc = new System.Net.WebClient();
wc.Credentials = System.Net.CredentialCache.DefaultCredentials;
wc.UseDefaultCredentials = true;
wc.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
string downloadUrl = SPContext.Current.Site.Url + i.Key;
Uri uri = new Uri(downloadUrl);
AppClass.WriteToLog(string.Format("downloadUrl: {0}\nUri.AbsoluteUri: {1}", downloadUrl, uri.AbsoluteUri));
byte[] img = wc.DownloadData(uri.AbsoluteUri);
if (!wc.ResponseHeaders.Get(0).Contains("OK"))
{
AppClass.WriteToLog("Unable to donwload the file");
}
zip.AddEntry(i.Value, img);
}
zip.Save(Response.OutputStream);
}
现在我还有另一个问题。The remote server returned an error: (403) Forbidden