1

Possible Duplicate:
How to simulate browser HTTP POST request and capture result in C#

I have the following form in my web page...

<input type="text" tabindex="1" id="username" name="username" size="30"/>
<input type="password" tabindex="2" id="password" name="password" size="30"/>

I use these to log into my website.
But I want to provide this functionality in my application as well.
How do I use a HTTP web request to send the user and password to my verification page?

Community
  • 1
  • 1
Vercas
  • 8,667
  • 15
  • 63
  • 105

3 Answers3

8

Try using the WebClient class. example:

var url = @"..."; 
var nvc = new System.Collections.Specialized.NameValueCollection();
nvc.Add("username", "username");
nvc.Add("password", "password");
var client = new System.Net.WebClient();
var data = client.UploadValues(url, nvc);
var res = System.Text.Encoding.ASCII.GetString(data);
Console.WriteLine(res);
Console.ReadLine();
James Skemp
  • 7,696
  • 9
  • 60
  • 101
The Mask
  • 16,429
  • 36
  • 107
  • 177
2

Scott Hanselman has a great post about this:

http://www.hanselman.com/blog/HTTPPOSTsAndHTTPGETsWithWebClientAndCAndFakingAPostBack.aspx

(quite similar to @The Mask response, but a bit more verbose on the explanation)

mtazva
  • 1,007
  • 9
  • 13
1

How to simulate browser HTTP POST request and capture result in C#

Community
  • 1
  • 1
jaltiere
  • 1,060
  • 6
  • 11