Joseph Michael Pesch
VP Programming

C# Http Remote Page Post

by 30. August 2013 14:15
NameValueCollection Inputs = new NameValueCollection();
Inputs.Add("param1", "something");
Inputs.Add("param2", "somethingelse");
Http.RemotePost("http://google.com", Inputs);

using System.Collections.Specialized;
using System.Net;

public static class Http
{
  public static void RemotePost(string Url, NameValueCollection Inputs)
  {
    System.Web.HttpContext.Current.Response.Clear();
    System.Web.HttpContext.Current.Response.Write("<html><head>");
    System.Web.HttpContext.Current.Response.Write(string.Format("</head><body onload=\"document.{0}.submit()\">", "form1"));
    System.Web.HttpContext.Current.Response.Write(string.Format("<form name=\"{0}\" method=\"{1}\" action=\"{2}\" >", "form1", "post", Url));
    for (int i = 0; i < Inputs.Keys.Count; i++)
    {
      System.Web.HttpContext.Current.Response.Write(string.Format("<input name=\"{0}\" type=\"hidden\" value=\"{1}\">", Inputs.Keys[i], Inputs[Inputs.Keys[i]]));
    }
    System.Web.HttpContext.Current.Response.Write("</form>");
    System.Web.HttpContext.Current.Response.Write("</body></html>");
    System.Web.HttpContext.Current.Response.End();
  }
}

Tags:

C#

Comments are closed