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,931
  • 15
  • 66
  • 106

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
  • 8,018
  • 9
  • 64
  • 107
The Mask
  • 17,007
  • 37
  • 111
  • 185
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,005
  • 9
  • 13
1

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

Community
  • 1
  • 1
jaltiere
  • 1,068
  • 6
  • 11