Reputation: 32986
Is there a standard way to read an XML file from a website that uses forms based authentication? We want to read the file into a desktop app.
thanks - dave
Upvotes: 0
Views: 441
Reputation: 1039298
If you are talking about ASP.NET Forms Authentication this will be a two step process:
Here's an example using a custom WebClient:
public class CookieAwareWebClient : WebClient
{
public CookieContainer Cookies { get; private set; }
public CookieAwareWebClient()
{
Cookies = new CookieContainer();
}
protected override WebRequest GetWebRequest(Uri address)
{
var request = base.GetWebRequest(address) as HttpWebRequest;
request.CookieContainer = Cookies;
return request;
}
}
class Program
{
static void Main()
{
using (var client = new CookieAwareWebClient())
{
client.UploadValues("http://www.foo.com/login.aspx", new NameValueCollection
{
{ "username", "foo" },
{ "password", "secret" },
});
client.DownloadFile("http://www.foo.com/foo.xml", "foo.xml");
}
}
}
Of course in the real life things might be more complex because depending on the site you might need to send along ViewState and other ASP.NET specific crap along with the request.
Upvotes: 2
Reputation: 41569
You're going to have to use HTTPWebRequest/HTTPWebResponse in roughly the following steps:
1: Use a request to submit the username and password to the website 2: Save the Cookies (I assume the cookies will contain the ok that the login worked) 3: Use another request, including these cookies, to get the XML.
To find the code that for the initial request you will need to look at the source code of the login page to see the submission action, and then reproduce this through your request. You may be able to use fiddler or firebug etc to help working this out.
Upvotes: 1
Reputation: 1319
post the credentials in an http request, on the response there will be an authentication cookie that you have to reuse for your next requests
Upvotes: 1