简体   繁体   English

将数据发布到网址并重定向

[英]Post Data To Url and Redirect

I need to post data to a url and redirect to the same. 我需要将数据发布到URL并重定向到相同的URL。 Presently i am using the code: 目前我正在使用代码:

using (WebClient client = new WebClient())
{
    byte[] response =
    client.UploadValues("https://website.com/target.aspx", new NameValueCollection()
    {
        { "param1", 1 },
        { "param2", 0 }
    });

    string result = System.Text.Encoding.UTF8.GetString(response);
}

I am getting html string as response. 我正在获取html字符串作为响应。 I need to redirect to the page " https://website.com/target.aspx ". 我需要重定向到页面“ https://website.com/target.aspx ”。

You cannot redirect using post, but instead you can make a normal get redirect Response.Redirect("https://website.com/target.aspx?s=1"); 您不能使用post进行重定向,而是可以进行常规的get重定向Response.Redirect("https://website.com/target.aspx?s=1"); with the query param s=1 (submit true). 查询参数s = 1(提交true)。 Now when the target.aspx page opens you may use javascript to check the query parameters and if the s param is 1 you use document.getElementById("idofyourform").submibt(); 现在,当target.aspx页面打开时,您可以使用javascript检查查询参数,如果s参数为1,则可以使用document.getElementById("idofyourform").submibt(); this will post the form. 这将发布表格。

In addition if you want also to fill the form values you may add other parameters to your query string. 另外,如果您还想填写表单值,则可以在查询字符串中添加其他参数。 They will get read and used to fill the form data in the page you redirect (target page). 它们将被读取并用于在您重定向的页面(目标页面)中填写表单数据。 Then you check the s variable (or any other whatever you name it) and do the automatic post! 然后,您检查s变量(或其他任何您命名的变量)并执行自动发布!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM