There are a number of reasons why you would want to send form data to a webpage but i was just trying to automate the process of logging into a website. The website itself presents a form where you need to enter a valid username and password.
The following links will help you to build a small application in C# to do just that:
Note that the method to send post data to an ASP.NET is quite different because you need to have a consistent viewstate information. Also, if you are trying to send data to an ASP.NET page, you will have to set the EnableEventValidation attribute on the page you are requesting to false as follows:
<%@ Page Language=”C#” EnableEventValidation=”false” %>
If you don’t do that, you will get an error saying that the postback data or callback is invalid because the EnableEventValidation checks that the request was made by a control on the page and since in this case it will be an external request, it will not validate and fail altogether.
Another thing i’ve noticed is that if you use set objRequest.CookieContainer = new CookieContainer() in your first request (where you are trying to log), your subsequent requests for pages which require authentication will fail. You will have to declare a global CookieContainer as follows:
CookieContainer _cookies = new CookieContainer();
objRequest.CookieContainer = _cookies;
When you get the ResponseStream back, it will automatically populate the _cookies variable for you. Therefore you can use it for subsequent web requests and it will authenticate properly.
The facility to perform such automated tasks is unbelievable and can be used for a lot of useful things – just think about it and you’ll see!