Как я могу сделать редирект с переменными поста - PullRequest
10 голосов
/ 20 апреля 2011

Я должен сделать перенаправление и отправить на другую страницу значение переменных a и p. Я не могу использовать метод GET, как: http://urlpage?a=1&p=2. Я должен отправить их по почте. Как я могу отправить их без использования формы из c #?

Ответы [ 5 ]

5 голосов
/ 16 июня 2015

Этот класс оборачивает форму. Отчасти хакерский, но это работает. Просто добавьте значения post в класс и вызовите метод post.

  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();
    }
}
1 голос
/ 20 апреля 2011
0 голосов
/ 10 апреля 2019

Приведенный ниже код работал для меня в Billdesk PG Integration.Большое спасибо.

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 голосов
/ 20 апреля 2011

Эта ссылка объяснит вам, как сделать следующее?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]
0 голосов
/ 20 апреля 2011

Используя WebClient.UploadString или WebClient.UploadData, вы можете легко отправлять данные на сервер. Я покажу пример использования UploadData, поскольку UploadString используется так же, как DownloadString.

byte[] bret = client.UploadData("http://www.website.com/post.php", "POST",
          System.Text.Encoding.ASCII.GetBytes("field1=value1&amp;field2=value2") );

string sret = System.Text.Encoding.ASCII.GetString(bret);

больше: http://www.daveamenta.com/2008-05/c-webclient-usage/

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...