views:

69

answers:

1

I try to download a file from https site and every time the file is saved to my machine it is only 1KB. The file is supposed to be 1MB. I am using Webclient.

string strFile = @"c:\myfile.txt";
WebClient wc = new WebClient();
wc.Credentials = new System.Net.NetworkCredential("userid", "pw");
wc.DownloadFile("https://www.mysite.come/myfile.txt", strFile);

Do I miss anything?

A: 

AFAIK WebClient by default does not put the User-Agent string, this could annoy servers; try

wc.Headers.Add("User-Agent", "XXX");

where you can pick up XXX from here.

ShinTakezou
What is XXX please? Is it https address?
I modified the codes and it seems to give me the information file, not the file I want.string strFile = @"c:\myfile.txt";WebClient wc = new WebClient();wc.Headers.Add("User-Agent", "https://www.mysite.come/");wc.Credentials = new System.Net.NetworkCredential("userid", "pw");wc.DownloadFile("https://www.mysite.come/myfile.txt", strFile);
no XXX is the user agent string, not the address: it's how a browser identifies itself to a http server; scripts may decide to "block" requests according to the user agent. The user agent string looks like "Opera/9.80 (X11; Linux i686; U; en-GB) Presto/2.2.15 Version/10.10" or whatever. It is better if it matches the one of a real browser
ShinTakezou