I want to navigate to a website using pre-defined cookies,
Add some text to couple of input type="text" and submit the form using the submit button.
I know it can be done, but I can’t find how.
I already tried to send the POST data to the page, but I have to click the button for the action to be executed.
Here is my code:
static String readHtmlPage(string url)
{
//setup some variables
String username = "demo";
String password = "password";
String firstname = "John";
String lastname = "Smith";
//setup some variables end
String result = "";
String strPost = "username=" + username + "&password=" + password + "&firstname=" + firstname + "&lastname=" + lastname;
StreamWriter myWriter = null;
HttpWebRequest objRequest = (HttpWebRequest)WebRequest.Create(url);
objRequest.Headers["Cookie"] = "sid=0";
objRequest.Headers["Cookie"] = "username=0";
objRequest.Method = "POST";
objRequest.ContentLength = strPost.Length;
objRequest.ContentType = "application/x-www-form-urlencoded";
try
{
myWriter = new StreamWriter(objRequest.GetRequestStream());
myWriter.Write(strPost);
}
catch (Exception e)
{
return e.Message;
}
finally
{
myWriter.Close();
}
HttpWebResponse objResponse = (HttpWebResponse)objRequest.GetResponse();
using (StreamReader sr =
new StreamReader(objResponse.GetResponseStream()))
{
result = sr.ReadToEnd();
// Close and clean up the StreamReader
sr.Close();
}
return result;
}
static void Main(string[] args)
{
Console.Write(readHtmlPage("http://www.ggogle.com/"));
}
My suggestion, which I’ve done in the past is to:
– Use Fiddler and hit the site with your browser and fill out the form like you would normally.
– Fiddler will record the request/response and you can copy the post data string and replace any values you need there and use HttpWebRequest/HttpWebResponse to programmatically do the POST and get the response.
post data through httpWebRequest
Example: Here is the post data I captured when I submitted the last comment.
comment=When+you+collect+the+recorded+POST+string+you+can+swap+out+the+key+value+pairs+in+there+before+you+make+the+request.+When+the+OnClick+event+fires+it+will+POST+data+to+the+server%2C+this+is+what+you+need+to+recreate+nothing+with+the+javascript.&fkey=62a7d57a52ee7fa723413a2e1dbe7e71You can then pass this string to the url that the POST was against and use HttpWebRequest to recreate it.
You also need to make sure you are URL Encoding your values in the post string.