Login to the Page with Httpwebrequest

Login to the page with HttpWebRequest

Make a new default.aspx, and put this in the code behind: I cant test any further based on your current question, because you didn't include a valid username/password.

    using System; 
using System.Web;
using System.Net;
using System.IO;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Foo
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create("http://sso.bhmobile.ba/sso/login");
req.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.0.3705;)";
req.Method = "POST";
req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
req.Headers.Add("Accept-Language: en-us,en;q=0.5");
req.Headers.Add("Accept-Encoding: gzip,deflate");
req.Headers.Add("Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7");
req.KeepAlive = true;
req.Headers.Add("Keep-Alive: 300");
req.Referer ="http://sso.bhmobile.ba/sso/login";

req.ContentType = "application/x-www-form-urlencoded";

String Username = "username";
String PassWord = "Password";

StreamWriter sw = new StreamWriter(req.GetRequestStream());
sw.Write("application=portal&url=http%3A%2F%2Fwww.bhmobile.ba%2Fportal%2Fredirect%3Bjsessionid%3D1C568AAA1FB8B5C757CF5F68BE6ECE65%3Ftype%3Dssologin%26url%3D%2Fportal%2Fshow%3Bjsessionid%3D1C568AAA1FB8B5C757CF5F68BE6ECE65%3Fidc%3D1023278&realm=sso&userid=" + Username + "&password=" + password + "&x=16&y=11");
sw.Close();

HttpWebResponse response = (HttpWebResponse)req.GetResponse();

StreamReader reader = new StreamReader(response.GetResponseStream());
string tmp = reader.ReadToEnd();

foreach (Cookie cook in response.Cookies)
{
tmp += "\n" + cook.Name + ": " + cook.Value;
}

Response.Write(tmp);
Response.End();

}
}
}

C# login to website using HTTPWebRequest

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

How to use HttpWebRequest to login to website?

You need to create and set a CookieContainer on the request. Without that your request wont succeed. Looks like the page is sending cookies with the response.

HTTP/1.1 200 OK
Date: Mon, 31 Jan 2011 23:53:23 GMT
Server: Apache
Expires: Mon, 31 Jan 2011 23:53:23 GMT
Vary: Cookie
Last-Modified: Mon, 31 Jan 2011 23:53:23 GMT
ETag: "ad806aa693ed8187c278f0fadfa92d01"
Cache-Control: max-age=0
Content-Type: text/html; charset=utf-8
Set-Cookie: sailonlinesid=bcc2b5fe9980df3e741e8fe7279d61d4; Domain=.sailonline.org; expires=Mon, 14-Feb-2011 23:53:23 GMT; Max-Age=1209600; Path=/
Connection: close


Related Topics



Leave a reply



Submit