471,624 Members | 1,937 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,624 software developers and data experts.

Post Forms Programmatically

Hello,

I would like to know how to post form data programmatically. The idea is
to get the intranet web page, programmatically entre the username and
password in a login form, post it and be redirected to the logged in
page. So far I have found the code to post data in a form, but I am not
sure if this is the correct way to do it.

Thank you in advance

string url = "http://intranet/app";
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
string proxy = null;

//string data = String.Format("USER={0}&PASSWORD={1}", "user",
"password");
byte[] buffer = Encoding.UTF8.GetBytes(data);

req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = buffer.Length;
req.Proxy = new WebProxy(proxy, true); // ignore for local
addresses
req.CookieContainer = new CookieContainer();
Stream reqst = req.GetRequestStream(); // add form data to request
stream
reqst.Write(buffer, 0, buffer.Length);
reqst.Flush();
reqst.Close();

*** Sent via Developersdex http://www.developersdex.com ***
Aug 19 '08 #1
2 2367
David wrote:
I would like to know how to post form data programmatically. The idea is
to get the intranet web page, programmatically entre the username and
password in a login form, post it and be redirected to the logged in
page. So far I have found the code to post data in a form, but I am not
sure if this is the correct way to do it.
string url = "http://intranet/app";
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
string proxy = null;

//string data = String.Format("USER={0}&PASSWORD={1}", "user",
"password");
byte[] buffer = Encoding.UTF8.GetBytes(data);

req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = buffer.Length;
req.Proxy = new WebProxy(proxy, true); // ignore for local
addresses
req.CookieContainer = new CookieContainer();
Stream reqst = req.GetRequestStream(); // add form data to request
stream
reqst.Write(buffer, 0, buffer.Length);
reqst.Flush();
reqst.Close();
The looks OK when quickly reading it.

What is the problem ?

Arne
Aug 19 '08 #2
Hi Arne,

The problem is that when I post the data to the form, I need to open
another URL that is password protected programmatically (hence sending
the user name and password first in the login page) and I am not sure
what to do after I run the code that I posted.

Thank you in advance

*** Sent via Developersdex http://www.developersdex.com ***
Aug 21 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Dan Greenblatt | last post: by
1 post views Thread by Lumpierbritches | last post: by
1 post views Thread by Anubis Cain Dante | last post: by
3 posts views Thread by Peter | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.