Prev: Deadly embrace between SetUnhandledExceptionFilter() and thread rundown(???) in Server 2003
Next: Get Return value when executing shell cmd using vc++
From: Gibson_junk on 19 Jan 2010 22:27 Hi, I am learning C++ for the past month and am interested in HTTPWebRequest for making a application to go along with my website. I have some code here that works for logging in to my site but i was wonder how to make it easier to use for when i am requesting data over and over. I use this to login and get that pages html. As you can see it all happens inside of a buttonclick event. How can i put this more professionally. not only from requesting login but for requesting information off of different pages. String^ user = gcnew String(username->Text); String^ pass = gcnew String(password->Text); try { CookieContainer^ ckie = gcnew CookieContainer(); Uri^ url = gcnew Uri("http://beta.rapidpremium.net/ login.php"); String^ data = "username=" + user + "&password=" + pass + "&login="; HttpWebRequest^ request = dynamic_cast<HttpWebRequest^> (WebRequest::Create(url)); request->Method = "POST"; request->CookieContainer = ckie; request->Referer = "http://beta.rapidpremium.net/usercp.php"; request->ContentLength = data->Length; request->ContentType = "application/x-www-form- urlencoded"; request->AllowAutoRedirect = true; StreamWriter^ writer = gcnew StreamWriter(request- >GetRequestStream()); writer->Write(data); writer->Close(); HttpWebResponse^ response = dynamic_cast<HttpWebResponse^>(request->GetResponse()); StreamReader^ reader = gcnew StreamReader(response- >GetResponseStream()); String^ tmp = reader->ReadToEnd(); response->Close(); htmlcode->Clear(); htmlcode->AppendText(tmp); } catch (Exception^ ex) { //outbox->Text = ex->ToString(); MessageBox::Show(ex->ToString(),"Connection Error"); } |