使用POST方法将表单数据从一个URL传递到另一个URL

时间:2017-11-08 10:35:20

标签: c# asp.net http-post

我有2个网站www.domain1.com& www.domain2.com。我想使用POST方法使用下面的代码将一些信息从domain1传递到domain2。但我在domain2中没有得到任何东西。我做错了什么?

private void btnRedirect_Click(object sender, EventArgs e)
{
    string strUsername = "Some Text Values Here";
    string outputHTML = "<html>";
    outputHTML += "<head>";
    outputHTML += "<title>Test</title>";
    outputHTML += "</head>";
    outputHTML += "<body>";
    outputHTML += "<center><h1>Please do not refresh this page...</h1></center>";
    outputHTML += "<form method='post' action='http://localhost:63064/userInfo.aspx' name='f1'>";
    outputHTML += "<input type='text' name='myUsername' value='" + strUsername + "' />";
    outputHTML += "<script type='text/javascript'>";
    outputHTML += "document.f1.submit();";
    outputHTML += "</script>";
    outputHTML += "</form>";
    outputHTML += "</body>";
    outputHTML += "</html>";
    Response.Write(outputHTML);
}

在domain2中我有beolow代码

ASPX

<form id="form1" runat="server" name="f1"></form>

背后的代码

protected void Page_Load(object sender, System.EventArgs e)
{
    string ID = string.Empty;
    if (Request.Form.Count > 0) {
        ID = Request.Form("myUsername");
    }
    ID = (ID.Trim());

    Response.Write(ID);
}

更新

Using WebClient to POST Data to another url
string strUsername = username.Text;
using (WebClient client = new WebClient()) {
    dynamic reqparm = new System.Collections.Specialized.NameValueCollection();
    reqparm.Add("myUsername", strUsername);
    byte[] responsebytes = client.UploadValues("http://localhost:63064/userInfo.aspx", "POST", reqparm);
    string responsebody = Encoding.UTF8.GetString(responsebytes);
    Response.Redirect("/userInfo.aspx");
}

0 个答案:

没有答案