Подтвердить что ты не робот

Как я могу сделать переадресацию с пост-переменными

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

4b9b3361

Ответ 1

Этот класс обертывает форму. Вид хаки, но он работает. Просто добавьте значения 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();
    }
}

Ответ 3

Используя WebClient.UploadString или WebClient.UploadData, вы можете легко отправлять POST-данные на сервер. Ill показать пример с использованием 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/

Ответ 4

Эта ссылка объясняет вам, как сделать следующее? 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]