3

I am trying to login to this website: https://lms.nust.edu.pk/portal/login/index.php Here's my code:

   const string uri = "https://lms.nust.edu.pk/portal/login/index.php";
    HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(uri);
    wr.KeepAlive = true;
    wr.Method = "POST";
    wr.AllowAutoRedirect = false;
    wr.Proxy = p;
    wr.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
    wr.ContentType = "application/x-www-form-urlencoded";
    wr.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.111 Safari/537.36";
    string pdata = "username=USERNAME&password=PASSWORD";
    byte[] data = UTF8Encoding.UTF8.GetBytes(pdata);
    wr.ContentLength = data.Length;
    CookieContainer cookie = new CookieContainer();
    wr.CookieContainer = cookie;
    using (Stream poststream = wr.GetRequestStream())
    {
        poststream.Write(data, 0, data.Length);
    }
    HttpWebResponse wp = (HttpWebResponse)wr.GetResponse();
    wr.CookieContainer.Add(wp.Cookies);

Its not going past the login page. Here's what I know is happening. There are no cookies in the wp.Cookies. I debugged the code, which told me that even though I have the uri specified and have set AllowAutoRedirect=false, its not going to that uri and another url. I don't know why or how. Can somebody help me?

1

1 Answer 1

8

This should solve your problem, or point you in the right direction at least, you may also want to download Fiddler to help debug HttpRequests...

string param = "username=MyUserName&password=123456";
string url = "https://lms.nust.edu.pk/portal/login/index.php";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.ContentLength = param.Length;
request.ContentType = "application/x-www-form-urlencoded";
request.CookieContainer = new CookieContainer();

using (Stream stream = request.GetRequestStream())
{
    byte[] paramAsBytes = Encoding.Default.GetBytes(param);
    stream.Write(paramAsBytes, 0, paramAsBytes.Count());
}

using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
    foreach (var cookie in response.Cookies)
    {
        var properties = cookie.GetType()
                               .GetProperties()
                               .Select(p => new 
                               { 
                                   Name = p.Name, 
                                   Value = p.GetValue(cookie) 
                               });

        foreach (var property in properties)
        {
            Console.WriteLine ("{0}: {1}", property.Name, property.Value);
        }
    }
}

And the response I got was...

Comment: 
CommentUri: 
HttpOnly: False
Discard: False
Domain: lms.nust.edu.pk
Expired: False
Expires: 01/01/0001 00:00:00
Name: MoodleSession
Path: /
Port: 
Secure: False
TimeStamp: 31/10/2014 00:13:58
Value: f6ich1aa2udb3o24dtnluomtd3
Version: 0

So cookies are definitely being returned as well...

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.