Im trying to post login data to a website so then I can grab information off of the website that you must be logged in to do so. So far my code post the data to the fields they are assigned to but the data isn't actually being submitted. The response I get back is the source code with the values I entered in the fieldID. Any idea how to get the fields to be submitted so then I can check the page if the user is logged in?
C#:
using System;
using System.IO;
using System.Net;
using System.Text;
using System.Collections.Specialized;
using System.Web;
using System.Collections;
namespace GetDataAfterLogin
{
public class Program
{
public class CookieAwareWebClient : WebClient
{
private CookieContainer cookie = new CookieContainer();
protected override WebRequest GetWebRequest(Uri address)
{
Uri Address = address;
Address = new Uri(@"http://yppedia.puzzlepirates.com/de/index.php?title=Spezial:Anmelden&action=submitlogin&type=login");
WebRequest request = base.GetWebRequest(Address);
if (request is HttpWebRequest)
{
(request as HttpWebRequest).CookieContainer = cookie;
}
return request;
}
}
static void Main(string[] args)
{
int counter = 0;
string UserName;
Console.WriteLine("Enter Password");
var Password = Console.ReadLine();
System.IO.StreamReader readfile = new System.IO.StreamReader(@"C:\ypp\UserNames.txt");
System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\ypp\1.txt");
System.IO.StreamWriter file1 = new System.IO.StreamWriter(@"C:\ypp\2.txt");
var client = new CookieAwareWebClient();
//client.BaseAddress = @"http://yppedia.puzzlepirates.com/de/";
var loginData = new NameValueCollection();
while ((UserName = readfile.ReadLine()) != null)
{
loginData.Add("wpName", UserName);
loginData.Add("wpPassword", Password);
byte[] response = client.UploadValues(@"http://yppedia.puzzlepirates.com/de/index.php?title=Spezial:Anmelden&action=submitlogin&type=login", "POST", loginData);
string responsebody = Encoding.UTF8.GetString(response);
//Write response to a text file for troubleshooting purposes
//file.WriteLine(Encoding.ASCII.GetString(response));
//Check to see if the login was successful or unsuccesful
if (responsebody.Contains("Benutzer:" + UserName)) // Checks the code before user + user to see if logged in
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Login Successful");
Console.ResetColor();
}
if (responsebody.Contains("Benutzernamen angeben")) //German code for Invalid password which is displayed on bad login
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Login Failed");
Console.ResetColor();
}
if (UserName == null)
{
file.Close();
}
counter++;
loginData.Clear();
}
readfile.Close();
Console.WriteLine("End of file reached");
Console.ReadLine();
}
}
}