Programmatic HTML POST with C # .NET 1.1

I am trying to integrate a Moneris monetized paid page in my .net 1.1 application with iFrame. I have done this many times before, but not with .net 1.1. It seems I can’t find a good resource for working with programmatic HTML message from 1.1. Any suggestions?

Thanks!

Editing: I think the HttpWebRequest solution does not work because you cannot do the redirection with POST. To integrate correctly with Moneris, you have a POST value for the amount, and then it is also redirected to the URL to which you sent the message. Sorry for the confusion.

+4
source share
4 answers

I am going to answer my own question here, just in case anyone else contacts him. Basically, I created my own HTTP handler (.ashx), which generates a .NET-independent HTML page with the form to which the POST is sent, to the URL that I want. Basically, a dynamically constructed HTML page. That way, I can dynamically pass the value of my quantity and then do the usual HTML POST.

0
source

You can use HttpWebRequest and HttpWebResponse for this.

For example, for POST for an HTML form that has two fields, username and password , you would do something like this:

 NameValueCollection nv = new NameValueCollection(); nv.Add("username", "bob"); nv.Add("password", "password"); string method = "POST"; // or GET string url = "http://www.somesite.com/form.html"; HttpStatusCode httpStatusCode; string response = SendHTTPRequest(nv, method, url, out httpStatusCode); public static string SendHTTPRequest(NameValueCollection data, string method, string url, out HttpStatusCode httpStatusCode) { StringBuilder postData = new StringBuilder(); foreach(string key in data) { postData.Append(key + "=" + data[key] + "&"); } if(method == "GET" && data.Count > 0) { url += "?" + postData.ToString(); } HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url); httpWebRequest.Method = method; httpWebRequest.Accept = "*/*"; httpWebRequest.ContentType = "application/x-www-form-urlencoded"; if(method == "POST") { using(Stream requestStream = httpWebRequest.GetRequestStream()) { using(MemoryStream ms = new MemoryStream()) { using(BinaryWriter bw = new BinaryWriter(ms)) { bw.Write(Encoding.GetEncoding(1252).GetBytes(postData.ToString())); ms.WriteTo(requestStream); } } } } return GetWebResponse(httpWebRequest, out HttpStatusCode httpStatusCode); } private static string GetWebResponse(HttpWebRequest httpWebRequest, out HttpStatusCode httpStatusCode) { using(HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse()) { httpStatusCode = httpWebResponse.StatusCode; if(httpStatusCode == HttpStatusCode.OK) { using(Stream responseStream = httpWebResponse.GetResponseStream()) { using(StreamReader responseReader = new StreamReader(responseStream)) { StringBuilder response = new StringBuilder(); char[] read = new Char[256]; int count = responseReader.Read(read, 0, 256); while(count > 0) { response.Append(read, 0, count); count = responseReader.Read(read, 0, 256); } responseReader.Close(); return response.ToString(); } } } return null; } } 
+2
source

See the HttpWebRequest class. This will allow you to create an HTTP request from scratch, which will allow you to include the data for publication in the URL you specify and receive a response.

+1
source

You want to use the httpwebrequest class, which has a method property that you can set to publish. Here is an example of documentation documentation.

http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.method(VS.71).aspx

0
source

All Articles