Pergunta

I try download file via WebClient, but it returned not all bytes from file. Original file has 45Kb, It download only 8kb from total file. It happened after I added user-agent header.

My code is:

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);
    }

Have you any ideas?

UPDATE: in the previous version url doesn't build correctly. I change my code to:

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);
}

And now I have another problem. The remote server returned an error: (403) Forbidden

Nenhuma solução correta

Outras dicas

Might be an error message or something like that. You're probably not really downloading bytes from the file. Try to read the result as a string to see if it's not an err message. Or try to get the HTTP response code from the WebClient.

This is a bad way to do it, but you should do some error checking like this:

> 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);
>          if (!wc.ResponseHeaders[0].Contains("OK"))
>          {
>            throw new Exception("Unable to donwload the file");
>          }
>          zip.AddEntry(i.Value, img);
>       }
>       zip.Save(Response.OutputStream); 
> }
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top