Reputation: 599
I see several responses to this issue however none seem to work in my case. Here is the situation.
I'm working with the API from thetvdb.com.
I am loading XML via API. If I run the application it will give me a 401 error. If I load the URL in IE first then run the application it loads fine.
I suspect its a cookie issue but I am not sure how to pass that cookie during my request below.
var _doc = new XmlDocument();
_doc.Load(_url);
Thanks in advance for your help.
Upvotes: 0
Views: 1101
Reputation: 599
After much reading and trial and error I have figured this out. I have tested this with several sites.
WebRequest _wr = WebRequest.Create(_url);
_wr.UseDefaultCredentials = true;
_wr.PreAuthenticate = true;
_wr.GetResponse();
//do your stuff
_wr.Dispose();
This seems to replicate the steps of me manually opening a browser and navigating to the page to prevent the 401 error when I launched my app. I hope this helps someone else as I understand the frustration.
Upvotes: 1
Reputation: 73564
HTTP error 401 means "unauthorized".
It means you need to contact the people providing the API and ask them how to authenticate.
It's different with every provider.
The right people to ask are the ones that provide the web services/API.
Upvotes: 2