executing a remote CGI page passing some values to it

teixeira

Regular
Joined
Apr 5, 2005
Messages
94
Location
LEIRIA-PORTUGAL
Hi,

I have a webpage called print.cgi, that basically prints in paper the text i send in a TextField inside a form called "my_form" with the POST method, and i would like to get this page in my c# windows form application and send to the instruction to print this page a text in the POST variable.
how can i do this?

Greetings
Tiago Teixeira
 
C#:
StringBuilder builder = new StringBuilder();
builder.Append("name=");
builder.Append(System.Web.HttpUtility.UrlEncode("Gill Bates"));
builder.Append("&country=");
builder.Append(System.Web.HttpUtility.UrlEncode("United States"));
 
byte[] data = Encoding.ASCII.GetBytes(builder.ToString());
 
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri("http://localhost:4087/post_test/form.aspx"));
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;
 
Stream stream = request.GetRequestStream();
stream.Write(data, 0, data.Length);
stream.Close();
 
WebResponse response = request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());
Console.WriteLine(reader.ReadToEnd());
reader.Close();
response.GetResponseStream().Close();
 
Hi,

Thanks 4 all, perfect. It rocks.
I just had a problem about the athentication but i added the right network credentials to the HttpWebRequest and i worked.

Thanks for the precious help, one more ;)

Regards,

Tiago Teixeira
 
Back
Top