Reputation: 457
I am using This code to download a file
private WebClient client;
client = new WebClient();
if (isBusy)
{
client.CancelAsync();
isBusy = false;
this.downloadButton.Text = "Download";
}
else
{
try {
Uri uri = new Uri(urlTextBox.Text);
this.downloadProgressBar.Value = 0;
client.Headers.Add("User-Agent: Other");
client.DownloadFileAsync(uri, "test.csv.zip");
this.downloadButton.Text = "Cancel";
isBusy = true;
}
catch (UriFormatException ex) {
MessageBox.Show(ex.Message);
}
}
But I am getting an error the error is
Download Not Complete: The remote server returned an error: (403) Forbidden.
I dont know why it is coming .
But when i use the uri for downloading in Free download Manager its working
I added this line
client.Headers.Add("User-Agent: Other");
but its still not working.
There would be great appreciation if someone could help me.
Thanks In Advance.
Upvotes: 1
Views: 9205
Reputation: 6451
It sounds like the free download manager you're using might be spoofing the referer headers whereas your implementation is not. The server might be restricting downloads of the file you are attempting to download to only be downloadable if the referer field is set to a specific value (i.e. a site on the server). Have you attempted:
client.Headers.Add("referer", uri);
It might be worth using Fiddler to see the difference between the request the download manager sends and yours and then amend yours until it works.
Edit:
I've tested the URL you provided and I've got it working locally by adding the following:
client.Headers.Add("Accept: text/html, application/xhtml+xml, */*");
client.Headers.Add("User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)");
You need to provide an "Accept" header otherwise the server does not know what your client wants / will accept. Here is my full anonimized sample application (using Sleep() for simplicity):
string url = "http://..."; // Change this to the full url of the file you want to download
string filename = "downloadedfile.zip"; // Change this to the filename you want to save it as locally.
WebClient client = new WebClient();
try
{
Uri uri = new Uri(url);
client.Headers.Add("Accept: text/html, application/xhtml+xml, */*");
client.Headers.Add("User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)");
client.DownloadFileAsync(uri, filename);
while (client.IsBusy)
{
System.Threading.Thread.Sleep(1000);
}
}
catch (UriFormatException ex)
{
Console.WriteLine(ex.Message);
}
Upvotes: 8
Reputation: 4263
The 403 Forbidden error is usually returned if the user does not have a permission to view/ download a particular content.
You have mentioned that it is working in Free download manager, but you have not mentioned whether you provided the authentication information in Free Download Manager(Yes, you can do that).
Anyway your user-agent may be a problem too, some websites does not allow clients with unknown user-agent, try adding a auseragent of a popular web browser and see if you can download the file.
IE 10.6 Mozilla/5.0 (compatible; MSIE 10.6; Windows NT 6.1; Trident/5.0; InfoPath.2; SLCC1; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; .NET CLR 2.0.50727) 3gpp-gba UNTRUSTED/1.0
try this line to add the above IE 10.6 user agent to your application
client.Headers.Add("User-Agent: Mozilla/5.0 (compatible; MSIE 10.6; Windows NT 6.1; Trident/5.0; InfoPath.2; SLCC1; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; .NET CLR 2.0.50727) 3gpp-gba UNTRUSTED/1.0");
You can find a whole list of useragent strings on the internet
Upvotes: 1