Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

post data through httpWebRequest

I need to "Post" some data to an external website using HttpWebRequest object from my application(desktop) and get a response back into my application through HttpWebResponse object. But the webpage on which i m posting data have textboxes which have dynamic names.

How can I get the name of those textboxes and post data in HttpWebResquest?

For example when I load the page the textbox name is like this U2FsdGVkX183MTQyNzE0MrhLOmUpqd3eL60xF19RmCwLlSiG5nC1H6wvtBDhjI3uM1krX_B8Fwc but when I refresh the page name change to this U2FsdGVkX182MjMwNjIzMPAtotst_q9PP9TETomXB453Mq3M3ZY5HQt70ZeyxbRb118Y8GQbgP8.

Thanks for any suggestions.

like image 227
user304901 Avatar asked Mar 31 '10 06:03

user304901


2 Answers

var request = WebRequest.Create("http://foo");
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
using (var writer = new StreamWriter(request.GetRequestStream()))
{
    writer.Write("field=value");
}
like image 175
Darin Dimitrov Avatar answered Oct 01 '22 11:10

Darin Dimitrov


You could et those names by XPath e.g. and user them like:

byte[]  data = new ASCIIEncoding().GetBytes("textBoxName1=blabla");
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create("http://localhost/myservlet");
httpWebRequest.Method = "POST";
httpWebRequest.ContentType = "application/x-www-form-urlencoded";
httpWebRequest.ContentLength = data.Length;
Stream myStream = httpWebRequest.GetRequestStream();
myStream.Write(data,0,data.Length);
myStream.Close();
like image 27
thelost Avatar answered Oct 01 '22 13:10

thelost