Hitesh Rana

Hitesh Rana

  • NA
  • 1
  • 0

Create a post request programmatically in ASP.NET

Apr 26 2009 3:00 PM
Hi,

I know its possible submit a form serverside without actually creating a html form. As you can see below:

string url = "http://websiteToSubmitTo";
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
string proxy = null;

string data = String.Format("parameter1={0}&parameter2={1}&parameter3={2}", parameter1, parameter2, parameter3);
byte[] buffer = Encoding.UTF8.GetBytes(data);

req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = buffer.Length;
req.Proxy = new WebProxy(proxy, true); // ignore for local addresses
req.CookieContainer = new CookieContainer(); // enable cookies

Stream reqst = req.GetRequestStream(); // add form data to request stream
reqst.Write(buffer, 0, buffer.Length);
reqst.Flush();
reqst.Close();

HttpWebResponse res = (HttpWebResponse)req.GetResponse();
   
Stream resst = res.GetResponseStream();
StreamReader sr = new StreamReader(resst);
string response = sr.ReadToEnd();

Question is how do you post a file over?

Thank you