WebClient - Am I logging in right, or being redirected right?
Hi
I'm trying to figure out a way to log into a new site that a 3rd party vendor has provided us by using a script.
I'm new to web related coding.
Its a ticketing site. We have to log into it.
If you attempt to load up a URL directly to a ticket, you are redirected to the login window. Once you log on, you can then view the ticket.
Code:
private void Form1_Load(object sender, EventArgs e)
{
string postData = "userName=Removed&password=Removed";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("End URL's location");
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = postData.Length;
request.Method = "POST";
request.AllowAutoRedirect = false;
Stream requestStream = request.GetRequestStream();
byte[] postBytes = Encoding.ASCII.GetBytes(postData);
requestStream.Write(postBytes, 0, postBytes.Length);
requestStream.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Console.WriteLine(new StreamReader(response.GetResponseStream()).ReadToEnd());
Console.WriteLine("Headers:");
Console.WriteLine(response.Headers.ToString());
string url = "End URL's location";
System.Diagnostics.Process.Start(url);
}
The info that I keep downloading is the login's page html code. Not the ticket page's code.
Console's results (made it a bit easier to read):
<input type="hidden" name="redirectAction" value="Removed_request_dashboard">
<input type="hidden" name="userName" value="Removed">
<input type="hidden" name="inc_id" value="5045176">
<input type="hidden" name="password" value="Removed">
There must be a tiny bit of code I'm missing. The login info is there, but its not logging me in. Or perhaps its logging me in but not directing me properly afterwards.
Re: WebClient - Am I logging in right, or being redirected right?
update...
The 'request' statements are now the following:
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = postData.Length;
request.Method = "POST";
request.AllowAutoRedirect = true;
request.PreAuthenticate = true;
request.Credentials = System.Net.CredentialCache.DefaultCredentials;
The page loads up and has the username & password filled out in the login page.
How do you actually 'submit' it?
The end result is that I don't intend to actually open up the browsers to get the tickets. I plan on downloading the html code which contains the ticket info and parse it.
Re: WebClient - Am I logging in right, or being redirected right?
string postData = "userName=Removed&password=Removed\r";
that worked...
I can directly log into a ticket now.
Still can't get the html code from it yet.
Re: WebClient - Am I logging in right, or being redirected right?
Well I don't know if this will work, as I don't know how the site stores login sessions, therefore this may not think your logged in.
Code:
private void Form1_Load(object sender, EventArgs e)
{
GetUrl("http://www.website.com/yourloginstring");
}
private void GetUrl(string url)
{
System.Net.WebClient Client = new System.Net.WebClient();
string storeHtml = Client.DownloadString(url);
}