Simple web request?

  • Thread starter Thread starter Brian Taylor
  • Start date Start date
B

Brian Taylor

I am trying to get some data from a web site that has form-based
authentication (username/password with submitt button).

So I just need to write software to go to the URL, enter the username and
password and download the information from the resulting page.

Does anyone know how to do this - that is entering the username and
password?

(I can tried to add CredentialCache object to a WebRequest but I think I
need to somehow Post the username and password information to the page.)

Brian
 
Hey Brian,

Use the HttpWebRequest class to make HTTP requests. Use the property HttpWebRequest.Method to specify that you wish to use HTTP POST. After that you can use the HttpWebRequest.GetRequestStream method to retrieve a stream to which you can write the post data such as username and password. Check the HTML of the web site to see the names of the input fields for the username and password.

See the docs for a short example: http://msdn.microsoft.com/library/d...thttpwebrequestclassgetrequeststreamtopic.asp

Regards, Jakob.
 
Thanks Jakob this look just what I need. I'll give it a go and post any
further problems if I get them.

Brian


Jakob Christensen said:
Hey Brian,

Use the HttpWebRequest class to make HTTP requests. Use the property
HttpWebRequest.Method to specify that you wish to use HTTP POST. After
that you can use the HttpWebRequest.GetRequestStream method to retrieve a
stream to which you can write the post data such as username and password.
Check the HTML of the web site to see the names of the input fields for the
username and password.
 
If all you want to do is post to a form, just use WebClient. It has an
UploadValues method, which will do the right thing for you.

feroze
===========
 
Well thanks Jakob and Feroze - with this information I managed to get both
methods working.
The WebClient method does not seem to pass the Cookie information (used to
keep the login context) between pages so I ended up using the Cookie
properties in HttpWebRequest and HttpWebResponse to do this.

Thanks,
Brian
 
I have the same problem......!
I need to keep the Session ID and then send a second Request wit the
username and password.

How do you finally integrate the second response?


string LoginCookies = wReq.CookieContainer.GetCookieHeader( ePageUri
).ToString();
NameValueCollection form = new NameValueCollection();
form.Add("email", "demo");
form.Add("password", "pass");

WebClient eClient = new WebClient();
eClient.Headers.Add("HTTP_USER_AGENT", "Mozilla/4.0 (compatible; MSIE
6.0; Windows NT 5.1; YComp 5.0.0.0; .NET CLR 1.0.3705)");
eClient.Headers.Add("Cookie", LoginCookies);

Byte[] responseData =
eClient.UploadValues(@"http://www.login.com/logincustomer.asp", "POST",
form);
string ResponseHtml = Encoding.ASCII.GetString( responseData );

But didnt work........ !!

Can you help me please !

Brian Taylorwrote:
Well thanks Jakob and Feroze - with this information I managed to get
both
methods working.
The WebClient method does not seem to pass the Cookie information (used to
keep the login context) between pages so I ended up using the Cookie
properties in HttpWebRequest and HttpWebResponse to do this.

Thanks,
Brian



Brian Taylorwrote:
Well thanks Jakob and Feroze - with this information I managed to get
both
 
I have the same problem......!
I need to keep the Session ID and then send a second Request wit the
username and password.

How do you finally integrate the second response?


string LoginCookies = wReq.CookieContainer.GetCookieHeader( ePageUri
).ToString();
NameValueCollection form = new NameValueCollection();
form.Add("email", "demo");
form.Add("password", "pass");

WebClient eClient = new WebClient();
eClient.Headers.Add("HTTP_USER_AGENT", "Mozilla/4.0 (compatible; MSIE
6.0; Windows NT 5.1; YComp 5.0.0.0; .NET CLR 1.0.3705)");
eClient.Headers.Add("Cookie", LoginCookies);

Byte[] responseData =
eClient.UploadValues(@"http://www.login.com/logincustomer.asp", "POST",
form);
string ResponseHtml = Encoding.ASCII.GetString( responseData );

But didnt work........ !!

Can you help me please !

Brian Taylorwrote:
Well thanks Jakob and Feroze - with this information I managed to get
both
 
I have the same problem......!
I need to keep the Session ID and then send a second Request wit the
username and password.

How do you finally integrate the second response?


string LoginCookies = wReq.CookieContainer.GetCookieHeader( ePageUri
).ToString();
NameValueCollection form = new NameValueCollection();
form.Add("email", "demo");
form.Add("password", "pass");

WebClient eClient = new WebClient();
eClient.Headers.Add("HTTP_USER_AGENT", "Mozilla/4.0 (compatible; MSIE
6.0; Windows NT 5.1; YComp 5.0.0.0; .NET CLR 1.0.3705)");
eClient.Headers.Add("Cookie", LoginCookies);

Byte[] responseData =
eClient.UploadValues(@"http://www.login.com/logincustomer.asp",
"POST", form);
string ResponseHtml = Encoding.ASCII.GetString( responseData );

But didnt work........ !!

Can you help me please !
 
Back
Top