How To Post Data From ASP.net To Another URL





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


public class RemotePost
{
NameValueCollection Inputs = new 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()
{
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Write("<html><head>");
HttpContext.Current.Response.Write(string.Format("</head><body onload=\"document.{0}.submit()\">", FormName));
HttpContext.Current.Response.Write(string.Format("<form name=\"{0}\" method=\"{1}\" action=\"{2}\" >", FormName, Method, Url));
int i = 0;
while (i < Inputs.Keys.Count)
{
HttpContext.Current.Response.Write(string.Format("<input name=\"{0}\" type=\"hidden\" value=\"{1}\">", Inputs.Keys[i],Inputs.Keys[i]));
i += 1;
}
HttpContext.Current.Response.Write("</form>");
HttpContext.Current.Response.Write("</body></html>");
HttpContext.Current.Response.End();
}
}


<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

protected void Page_Load(object sender, EventArgs e)
{

}

protected void Button1_Click(object sender, EventArgs e)
{
RemotePost obj = new RemotePost();
obj.Url = "http://localhost/Project/Default.aspx";
obj.Method = "Get";
obj.Add("test", "test");
obj.Post();
}
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Post Data To Another Application" /></div>
</form>
</body>
</html>


<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

protected void Page_Load(object sender, EventArgs e)
{
if (Request.Form["test"] != null)
{
Response.Write(Request.Form["test"].ToString());

}
}
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>

</div>
</form>
</body>
</html>

No comments:

Post a Comment