WebClient 403 Forbidden
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
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.
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;