English 中文(简体)
Programmatic HTML POST with C#.NET 1.1
原标题:
  • 时间:2009-11-13 19:15:22
  •  标签:
  • c#
  • .net
  • post

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.

最佳回答

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.

问题回答

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

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.

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





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签