Tag Archives: web browser control

Post Data using ASP.NET Web Browser Control

Hello People,

Its almost 2 AM here at my place, need a sound sleep from so many days but before that let me share this trick with you all.

Have you ever used ASP.NET Web Browser for your application, give it a try! its very useful. It really helped me sort out a big problem in one of my application where I previously was using HttpWebRequest but I got stuck when the page I was posting data to used a cookie checking before processing any request and the cookie was created using javascript.

As far as from my research I have found you cannot fire a javascript code on a page using HttpWebRequest, please correct me if I am wrong anywhere, anyways so this was my problem. Finally i came through Web Browser control and gave it a try. Because it works similar to any browser, client side script (javascript) aslo worked fine in it 🙂

Here is a small excerpt from my code

AxWebBrowser wb = new AxWebBrowser ();
object vPost = null;
object vHeaders = null;
object obj = null;
object url = “http://www.yoururl.com/yourpage”;
object cPostData = null;

cPostData = “var1=data1&var2=datra2&var3=data3&var4=data4”;

vHeaders = “Content-Type: application/x-www-form-urlencoded” + Convert.ToChar(10) + Convert.ToChar(13);
(You can add other headers as required)

vPost = ASCIIEncoding.ASCII.GetBytes((string)cPostData);
wb.Navigate2(ref url, ref obj, ref obj, ref vPost, ref vHeaders);

This is all it takes to post data programmatically to a page using Web Browser control in you desktop application.

Hope this helps you, if you have any queries please feel free to ask.