WebClient.DownloadData() returning 0 bytes

Go To StackoverFlow.com

0

I'm trying to download a PDF into a memory stream which I'm going to attach to a MailMessage. Here is my code:

string pdfUrl = "http://dev.domain.com/pdf+files/sample.pdf";
WebClient client = null;
MemoryStream stream;
try
{
    client = new WebClient();
    stream = new MemoryStream(client.DownloadData(pdfUrl));
    attachments.Add(new Attachment(stream, "Sample.pdf");
}
finally
{
    client.Dispose();
}

The byte[] returned from client.DownloadData(pdfUrl) has a length of 0.

Any ideas?

2012-04-05 18:00
by im1dermike


0

There are many different reasons why a download can work from a web browser but fail when attempted programmatically. These reasons include: missing cookies (including authentication tokens), mismatched method (POST vs. GET), missing form fields, different HTTP headers, problems with redirects, etc.

Take a look at this catalog of screen-scraping gotchas for more info on how to resolve.

If I had to guess about what's going wrong in your case, I'd guess either a missing cookie or HTTP header.

2012-04-08 21:43
by Justin Grant
Ads