Click here to Skip to main content
15,895,829 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi.

I have the username, password and url of the website. I want to login into the website automatically. I googled it but could not get the solution. Also used POST, WebRequest but couldn't log in. Could anyone please help and send me the code?
Posted
Comments
Sandip.Nascar 10-Oct-12 4:32am    
If the site uses HTTP POST to validate login, then only you can externally use the post method to login to the site.
chintanvp 11-Oct-12 2:48am    
string appURL = "http://blahblah.com";
string strPostData = String.Format("userName={0}&password={1}",
"username", "password");
// Setup the http request.
HttpWebRequest wrWebRequest = WebRequest.Create(appURL) as
HttpWebRequest;
wrWebRequest.Method = "post";
wrWebRequest.ContentLength = strPostData.Length;
wrWebRequest.ContentType = "application/x-www-form-urlencoded";
CookieContainer cookieContainer = new CookieContainer();
wrWebRequest.CookieContainer = cookieContainer;
// Post to the login form.
StreamWriter swRequestWriter = new
StreamWriter(wrWebRequest.GetRequestStream());
swRequestWriter.Write(strPostData);
swRequestWriter.Close();
// Get the response.
HttpWebResponse hwrWebResponse = (HttpWebResponse)wrWebRequest.GetResponse();
// Read the response
StreamReader srResponseReader = new
StreamReader(hwrWebResponse.GetResponseStream());
string strResponseData = srResponseReader.ReadToEnd();
srResponseReader.Close();

Response.Write(strResponseData);
return cookieContainer;

This is the Code that i am using for auto login.. please let me know if i am wrong anywhere
chintanvp 6-Nov-13 23:00pm    
Hi Sandip Nascar, Thank you for the reply. But when i try the code, it doesn't login nor the link gets open. Can you please help. Thanks.

1 solution

Hi Sandip , I am a new bie in ASP .net i just read through the code and tried to automate my webmail login via same, while i am getting 401 .

Could you please suggest me , where do i need to work on , also any help on other web methods , like i used to work with (web browser control in forms for login automation) would be much helpful.

Thank you


C#
<pre>

Uri uri1=new Uri("https://webmail.abc.com");

string strPostData = String.Format("username={0}&password={1}", "username", "passw123");
HttpWebRequest webreq1=WebRequest.Create(uri1) as HttpWebRequest;
webreq1.Method = "post";
webreq1.ContentLength = strPostData.Length;
webreq1.ContentType = "application/x-www-form-urlencoded";

CookieContainer cookieContainer = new CookieContainer();
webreq1.CookieContainer = cookieContainer;

StreamWriter web1_p = new StreamWriter(webreq1.GetRequestStream());
web1_p.Write(strPostData);
web1_p.Close();

HttpWebResponse Hr_resp = (HttpWebResponse)webreq1.GetResponse();
StreamReader sr_reader = new StreamReader(Hr_resp.GetResponseStream());
string weba = sr_reader.ReadToEnd();
TextBox1.Text = Andz.ToString();
sr_reader.Close();
 
Share this answer
 

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