i want to Log in to an Https site at console by passing my username & password via Post Data. But i'm am unable to do this. All i get is that page which deals with the cases of 'Incorrect Username/Password'. I Searched out a lot but unable to resolve this issue. I have successfully By Passed the SSL but still remain to the wrong page.. I'm sharing the code currently i'm dealing with...
CookieContainer CC = new CookieContainer();
WebRequest request = WebRequest.Create("https://the websit link is here!");
request.Proxy = null;
request.Credentials = CredentialCache.DefaultCredentials;
//allows for validation of SSL certificates
ServicePointManager.ServerCertificateValidationCallback += new System.Net.Security.RemoteCertificateValidationCallback(ValidateServerCertificate);
Console.WriteLine("\n");
Console.WriteLine("Enter your Email/Username :- ");
string username = Console.ReadLine();
Console.WriteLine("Enter your password :- ");
string password = Console.ReadLine();
string postData = String.Format("username={0}&password={1}", username, password);
HttpWebRequest getRequest = (HttpWebRequest)WebRequest.Create("https://the login page link of the website here! ");
getRequest.Method = WebRequestMethods.Http.Post;
getRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.121 Safari/535.2";
byte[] byteArray = Encoding.ASCII.GetBytes(postData);
Stream newStream = getRequest.GetRequestStream(); //open connection
newStream.Write(byteArray, 0, byteArray.Length); // Send the data.
newStream.Close();
HttpWebResponse getResponse = (HttpWebResponse)getRequest.GetResponse();
// getRequest.CookieContainer.Add(getResponse.Cookies);
string responseString = new StreamReader(getResponse.GetResponseStream()).ReadToEnd();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
Console.WriteLine("Reader Object(StreamReader): " + reader);
string responseStr = reader.ToString();
string responseFromServer = reader.ReadToEnd();
Console.WriteLine("\nWriting to file...");
StreamWriter file = new StreamWriter("E:\\I'M Response_FinalTry.html");
try
{
for (int i = 0; i < responseFromServer.Length; i++)
{
file.Write(responseFromServer[i]);
}
}
catch (Exception e)
{
Console.WriteLine("Caught Exception is: " + e);
}
finally
{
Console.WriteLine("Closing file...");
file.Close();
Console.WriteLine("Done..!!");
}
}
public static bool ValidateServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
return true;
}
i'm using Stream class to only save the response string/page from the server.. I don't have the server side code... Kindly, guide me in this issue.... :)