The following function can be used to perform a HTTP POST in Razor (C#). To use it simply, call the PostWebRequest(...) function with the appropriate parameters.
@functions{
// Function to help post HTTP request to web service
private string PostWebRequest(String url, String data, int timeout)
{
var buffer = System.Text.Encoding.UTF8.GetBytes(data);
System.Net.HttpWebRequest webRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
webRequest.Timeout = timeout;
webRequest.Method = "POST";
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.ContentLength = buffer.Length;
var requestStream = webRequest.GetRequestStream();
requestStream.Write(buffer, 0, buffer.Length);
requestStream.Flush();
requestStream.Close();
System.Net.HttpWebResponse webResponse = (System.Net.HttpWebResponse)webRequest.GetResponse();
var streamReader = new System.IO.StreamReader(webResponse.GetResponseStream());
return streamReader.ReadToEnd();
}
}
@{
PostWebRequest("http://myurl.com/page.aspx", "d1=val1&d2=val2", 30000);
}