Erebus
Erebus

Reputation: 2038

Programmatic HTML POST with C#.NET 1.1

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.

Upvotes: 4

Views: 2481

Answers (4)

Erebus
Erebus

Reputation: 2038

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.

Upvotes: 0

Kev
Kev

Reputation: 119806

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;
  }
}

Upvotes: 2

Timothy S. Van Haren
Timothy S. Van Haren

Reputation: 8966

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.

Upvotes: 1

Bernard Chen
Bernard Chen

Reputation: 6567

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(VS.71).aspx

Upvotes: 0

Related Questions