프로그램 자료/ASP.NET

코드비하인드에서 페이지 이동시 포스트로 데이터 보내기 asp.net code behind redirect with post data

motolies 2014. 12. 24. 11:55

출처 : http://forums.asp.net/t/1473766.aspx?doing+POST+in+code+behind



아래의 클래스를 추가 한 뒤


public class RemotePost

{

    private System.Collections.Specialized.NameValueCollection Inputs

    = new System.Collections.Specialized.NameValueCollection();

 

    public string Url = "";

    public string Method = "post";

    public string FormName = "form1";

 

    public void Add(string name, string value)

    {

        Inputs.Add(name, value);

    }

 

    public void Post()

    {

        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()\">", FormName));

 

        System.Web.HttpContext.Current.Response.Write(string.Format("<form name=\"{0}\" method=\"{1}\" action=\"{2}\" >",

 

        FormName, Method, 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();

    }

}

 

 

아래와 같이 사용한다


string URI = "http://" + Request.Url.Host + "/url";

 

RemotePost r = new RemotePost();

r.Url = URI;

r.Add("test", "value1test");

r.Post();