esak2000
asked on
Login with c# HttpWebRequest
I would like to automatically log into a site 'petwholesaler.com' and remain in the site using the HttpWebRequest browser class. I tried the following:
It doesn't work. Does anyone know what's the exact code I need to login?
CookieContainer _siteContainer;
string _login = "myUserName";
string _password = "myPassword";
string strPostData = String.Format("username={0}&password={1}", _login, _password);
// Setup the http request.
HttpWebRequest wrWebRequest = WebRequest.Create("http://www.petwholesaler.com/login.php?ref=express_order_form") as HttpWebRequest;
wrWebRequest.Method = "POST";
wrWebRequest.ContentLength = strPostData.Length;
wrWebRequest.ContentType = "application/x-www-form-urlencoded";
_siteContainer = new CookieContainer();
wrWebRequest.CookieContainer = _yahooContainer;
// Post to the login form.
using (StreamWriter swRequestWriter = new StreamWriter(wrWebRequest.GetRequestStream()))
{
swRequestWriter.Write(strPostData);
swRequestWriter.Close();
}
// Get the response.
HttpWebResponse response = (HttpWebResponse)wrWebRequest.GetResponse();
StreamReader myStreamReader = new StreamReader(response.GetResponseStream());
string responseString = myStreamReader.ReadToEnd();
StreamWriter swrite = new StreamWriter(@"PetWholesaler.html");
swrite.Write(responseString);
swrite.Close();
It doesn't work. Does anyone know what's the exact code I need to login?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.