I have to do a redirect and send to another page the value of variables a
and p
. I can't use the GET method like: http://urlpage?a=1&p=2
. I have to send them with the post method. How can I send them without use a form from c#?
Asked
Active
Viewed 2.9k times
11

Wai Ha Lee
- 8,598
- 83
- 57
- 92

Luca Romagnoli
- 12,145
- 30
- 95
- 157
-
Asp.Net? Mvc? What are you using? – Kees C. Bakker Apr 20 '11 at 09:18
-
could you accept or write your answer? – Leandro Bardelli Oct 22 '14 at 19:56
-
see this article http://stackoverflow.com/questions/16619065/curl-request-with-asp-net Hops it will help. – shihab mm Jun 16 '15 at 10:27
-
@LucaRomagnoli have you found your answers? – Pingpong Aug 30 '16 at 16:02
5 Answers
6
This class wraps the form. Kind of hacky but it works. Just add the post values to the class and call the post method.
public class RemotePost
{
private Dictionary<string, string> Inputs = new Dictionary<string, string>();
public string Url = "";
public string Method = "post";
public string FormName = "form1";
public StringBuilder strPostString;
public void Add(string name, string value)
{
Inputs.Add(name, value);
}
public void generatePostString()
{
strPostString = new StringBuilder();
strPostString.Append("<html><head>");
strPostString.Append("</head><body onload=\"document.form1.submit();\">");
strPostString.Append("<form name=\"form1\" method=\"post\" action=\"" + Url + "\" >");
foreach (KeyValuePair<string, string> oPar in Inputs)
strPostString.Append(string.Format("<input name=\"{0}\" type=\"hidden\" value=\"{1}\">", oPar.Key, oPar.Value));
strPostString.Append("</form>");
strPostString.Append("</body></html>");
}
public void Post()
{
System.Web.HttpContext.Current.Response.Clear();
System.Web.HttpContext.Current.Response.Write(strPostString.ToString());
System.Web.HttpContext.Current.Response.End();
}
}

Jaime Enrique Espinosa Reyes
- 91
- 1
- 4
-
This is good, but you should `HttpUtility.HtmlEncode` to escape your form parameters when building the HTML. – dana Sep 21 '15 at 16:43
0
The below code worked for me for Billdesk PG Integration. Thanks a lot.
public class RemotePost
{
private Dictionary<string, string> Inputs = new Dictionary<string, string>();
public string Url = "";
public string Method = "post";
public string FormName = "form1";
public StringBuilder strPostString;
public void Add(string name, string value)
{
Inputs.Add(name, value);
}
public void generatePostString()
{
strPostString = new StringBuilder();
strPostString.Append("<html><head>");
strPostString.Append("</head><body onload=\"document.form1.submit();\">");
strPostString.Append("<form name=\"form1\" method=\"post\" action=\"" + Url + "\" >");
foreach (KeyValuePair<string, string> oPar in Inputs)
strPostString.Append(string.Format("<input name=\"{0}\" type=\"hidden\" value=\"{1}\">", oPar.Key, oPar.Value));
strPostString.Append("</form>");
strPostString.Append("</body></html>");
}
public void Post()
{
System.Web.HttpContext.Current.Response.Clear();
System.Web.HttpContext.Current.Response.Write(strPostString.ToString());
System.Web.HttpContext.Current.Response.End();
}
}
0
Using WebClient.UploadString
or WebClient.UploadData
you can POST data to the server easily. I’ll show an example using UploadData, since UploadString is used in the same manner as DownloadString.
byte[] bret = client.UploadData("http://www.website.com/post.php", "POST",
System.Text.Encoding.ASCII.GetBytes("field1=value1&field2=value2") );
string sret = System.Text.Encoding.ASCII.GetString(bret);

Pranay Rana
- 175,020
- 35
- 237
- 263
-1
This link explain you how to do the following? http://msdn.microsoft.com/en-us/library/debx8sh9.aspx
using System.Net;
...
string HttpPost (string uri, string parameters)
{
// parameters: name1=value1&name2=value2
WebRequest webRequest = WebRequest.Create (uri);
//string ProxyString =
// System.Configuration.ConfigurationManager.AppSettings
// [GetConfigKey("proxy")];
//webRequest.Proxy = new WebProxy (ProxyString, true);
//Commenting out above required change to App.Config
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.Method = "POST";
byte[] bytes = Encoding.ASCII.GetBytes (parameters);
Stream os = null;
try
{ // send the Post
webRequest.ContentLength = bytes.Length; //Count bytes to send
os = webRequest.GetRequestStream();
os.Write (bytes, 0, bytes.Length); //Send it
}
finally
{
if (os != null)
{
os.Close();
}
}
try
{ // get the response
WebResponse webResponse = webRequest.GetResponse();
if (webResponse == null)
{ return null; }
StreamReader sr = new StreamReader (webResponse.GetResponseStream());
return sr.ReadToEnd ().Trim ();
}
return null;
} // end HttpPost
[edit]

sikender
- 5,883
- 7
- 42
- 80