Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello,
can anyone help me out, in login to a website(programatically) that is https. I have the username and password of that site. I am using HttpWebRequest and HttpWebResponse..

my code is as follow:-

CookieContainer cookies = new CookieContainer();
WebRequest loginRequest;
//Go to login page so that we get a cookie
loginRequest = (HttpWebRequest)HttpWebRequest.Create("https://sitename");
loginRequest.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes("" + TextBox1.Text + ":" + TextBox2.Text + "")));
loginRequest.UseDefaultCredentials = true;
loginRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)";
loginRequest.CookieContainer = cookies;
loginRequest.AllowAutoRedirect = false;

HttpWebResponse loginResponse = (HttpWebResponse)loginRequest.GetResponse();

//Now that we have the cookie we can do a proper login
HttpWebRequest doLogin = (HttpWebRequest)HttpWebRequest.Create("https://siteName");

//doLogin.UseDefaultCredentials = true;
doLogin.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)";
doLogin.Method = "POST";
doLogin.ContentType = "text/html;charset=UTF-8";
doLogin.AllowAutoRedirect = false;

using (StreamWriter sw = new StreamWriter(doLogin.GetRequestStream()))
{
sw.Write("usr=<username>&pass=" +TextBox2.Text + "&mc=1&guid=&dbnum=0&ic=1&task=dologin&app=library&x=32&y=10");

}

doLogin.CookieContainer = cookies;
doLogin.Referer = "https://secure.sbigeneral.in/Extranet/faces/premiumcalc_landingpage";

HttpWebResponse Response = (HttpWebResponse)doLogin.GetResponse();

string resphead = Response.Headers["Set-Cookie"];

string resp = Response.StatusCode.ToString();

using (StreamReader sr = new StreamReader(Response.GetResponseStream()))
{
string textresponse = sr.ReadToEnd();

}
Posted
Updated 18-Nov-14 18:27pm
v2
Comments
Kornfeld Eliyahu Peter 18-Nov-14 9:01am    
http://mattgemmell.com/what-have-you-tried/
ZurdoDev 18-Nov-14 9:09am    
Please click on improve question and post the relevant code and show us where you are stuck.

1 solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900