tags:

views:

382

answers:

5

Hello,

I'm trying to integrate the Moneris Hosted Pay Page into my .net 1.1 app with an iFrame. I've done this many times before, but not with .net 1.1. I can't seem to find a good resource for doing a programmatic HTML Post with 1.1. Any suggestions?

Thanks!

Edit: Poking around with the suggestions given, I'm realizing that the HttpWebRequest solution won't work because you can't do a redirect along with the POST. In order to integrate properly with Moneris, you've got to POST the amount value, and then also redirect to the URL you've POSTed to. Sorry for the confusion.

Edit: I'm going to answer my own question here just in case this gets referred to by someone else. Basically, I created a custom HTTP handler (.ashx) that generates a .NET independent HTML page with a form that POSTs to the URL I want. Basically, a dynamically built HTML page. This way I can dynamically pass my amount value, and then do a normal HTML POST.

A: 

You want to use the httpwebrequest class, which has a "method" property that can be set to post. There's an example in the documentation of the property.

http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.method%28VS.71%29.aspx

Bernard Chen
+1  A: 

See the HttpWebRequest class. This will allow you to build an HTTP request from scratch, which will allow you to include the data to post to the URL you specify, and get a response back.

Tim S. Van Haren
A: 

Check out this link for full details: http://en.csharp-online.net/HTTP%5FPost

Sonny Boy
+1  A: 

You can use the HttpWebRequest and HttpWebResponse classes to achieve this.

For example, to POST to 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;
  }
}
Kev
+1  A: 

I'm going to answer my own question here just in case this gets referred to by someone else. Basically, I created a custom HTTP handler (.ashx) that generates a .NET independent HTML page with a form that POSTs to the URL I want. Basically, a dynamically built HTML page. This way I can dynamically pass my amount value, and then do a normal HTML POST.

Erebus
This is the proper way to do an HTTP Post with .NET. If you use an HttpWebRequest, the server receives the response and must write that to the client. Then, any interactions with the resulting form may be recognized by Cross Site Scripting by the third party. By using a standard HTTP form post on the client side, the user's browser is redireceted to that third party site.
1nsane