Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebClient 403 Forbidden

Tags:

I can download this by hand in IE.

http://scholar.google.com/scholar.ris?q=info:j8ymU9rzMsEJ:scholar.google.com/&output=citation&hl=zh-CN&as_sdt=2000&oe=GB&ct=citation&cd=0

But, using follow code

WebClient client = new WebClient();
client.DownloadFile(address, filename);

Show Exception: 403 Forbidden

What's wrong? How can I do that?

others

http://scholar.google.com/scholar.ris?q=info:sskrpr5jlLwJ:scholar.google.com/&output=citation&hl=zh-CN&as_sdt=2000&oe=GB&ct=citation&cd=1

like image 487
Begtostudy Avatar asked Jul 17 '10 15:07

Begtostudy


People also ask

How do I fix 403 Forbidden in Java?

Double Check the Address. The most common reason for a 403 error is a mistyped URL. First, ensure that the address you are trying to access is for a web page or file, not a directory. For example, a regular URL would end in .com, .

Why 403 forbidden access is denied?

The HTTP 403 Forbidden response status code indicates that the server understands the request but refuses to authorize it. This status is similar to 401 , but for the 403 Forbidden status code, re-authenticating makes no difference. The access is tied to the application logic, such as insufficient rights to a resource.


2 Answers

Just add a simple line before you make your download:

string url = ... 
string fileName = ...

WebClient wb = new WebClient();
wb.Headers.Add("User-Agent: Other");   //that is the simple line!
wb.DownloadFile(url, fileName);

That's it.

like image 185
Borg8 Avatar answered Sep 17 '22 20:09

Borg8


403 may also be caused by TLS issues. To verify, you should check the text of the WebException.Response object.

     catch (WebException ex)
     {
        if (ex.Response != null)
        {
           var response = ex.Response;
           var dataStream = response.GetResponseStream();
           var reader = new StreamReader(dataStream);
           var details = reader.ReadToEnd();
        }
     }

If it is TLS then try adding this to your code to force TLS1.2.

For .net4:

ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;

For .net4.5 or later:

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

like image 29
Developer Wonk Avatar answered Sep 18 '22 20:09

Developer Wonk