0

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.... :)

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
  • Have you tried getting the 'postData' byte array in UTF8 and sending? Also, validate your content type. Check: http://msdn.microsoft.com/en-us/library/debx8sh9(v=vs.110).aspx – João Simões Jan 13 '14 at 09:45

1 Answers1

0

I believe you should change the following for your code to work:

getRequest.ContentType = "application/x-www-form-urlencoded";
byte[] byteArray = Encoding.UTF8.GetBytes(postData);

See: http://msdn.microsoft.com/en-us/library/debx8sh9(v=vs.110).aspx

Edit:

Is it possible to use the WebClient class for this?

using System.Net;
using System.Collections.Specialized;

using (var client = new WebClient())
{
    var values = new NameValueCollection();
    values["username"] = "user1";
    values["password"] = "12345";

    var response = client.UploadValues("https://www.mydomain.com/login.aspx", values);

    var responseString = Encoding.Default.GetString(response);
}

Source: HTTP request with post

Community
  • 1
  • 1
João Simões
  • 1,351
  • 1
  • 10
  • 20
  • Thanks, but actually i have applied this code but the problem is that it works fine for "facebook.com" and some of the other sites but i want it to work "on a Local Server". e.g a college's own server. So, that is the main issue. – Zeeshan Jan 20 '14 at 09:16
  • Are you using HTTPS for that server? It may not be related, but is the HTTPS certificate a trusted one? – João Simões Jan 22 '14 at 08:08
  • Yes definitely, it is Https and i want to be logged in to it through programmatically... I've successfully By-passed the certificate over Https... – Zeeshan Jan 22 '14 at 11:33
  • Would anyone guide me more in this case????? Signing in to an SSL Sites over a local (college) server????? – Zeeshan Jan 26 '14 at 17:58
  • 1
    Thanks to all who helped and i've got the point that i was missing. The issue was with the 'Post Data' and also some of the hidden fields.. Thank GOD i did it :) – Zeeshan Jan 30 '14 at 07:09