Tommy Unger
posted this on May 19, 2010 09:05 pm
This short ASP.Net C# code sample shows how you can submit your form data to Optify via our secure API.
<%@ Import Namespace="System.Net" %>
<%@ Import Namespace="System.IO" %>
<%@ Page Language="C#" Debug="true" %>
<script language="CS" runat="server">
void Page_Load(object sender, System.EventArgs e)
{
if(Request.Form["_opt_vid"] != null)
{
// Can post to http or https
string post_url = "http://service.optify.net/v2/form";
WebRequest postRequestForOptify = WebRequest.Create(post_url);
postRequestForOptify.Method = "POST"; // Optify currently only accepts post.
postRequestForOptify.ContentType="application/x-www-form-urlencoded";
// Put posted from Request.Form variables into a querystring like object for the WebRequest
ArrayList postVariables = new ArrayList();
foreach(string key in Request.Form)
{
postVariables.Add(key + "=" + HttpUtility.UrlEncode(Request.Form[key]));
}
string Parameters = String.Join("&",(String[]) postVariables.ToArray(typeof(string)));
postRequestForOptify.ContentLength = Parameters.Length;
// Add the posted parameters to the WebRequest
StreamWriter sw = new StreamWriter(postRequestForOptify.GetRequestStream());
sw.Write(Parameters);
sw.Close();
// Make the request and get the response
HttpWebResponse optifyPostResponse = (HttpWebResponse)postRequestForOptify.GetResponse();
StreamReader sr = new StreamReader(optifyPostResponse.GetResponseStream());
string optifyResponse = sr.ReadToEnd();
}
}
</script>