0

大家好,我是 C# 的新手,我正在尝试将一些隐藏字段发布到表单中,我尝试了所有找到的方法,但我似乎无法将参数发送到 aspx 表单,这些是我尝试过的编码的一部分

using (WebClient client = new WebClient())
        {
            NameValueCollection postData = new NameValueCollection() 
   { 
          { "s_transm", "TEST" },
          { "c_referencia", "TEST" }
   };

           var result =client.UploadValues(Parameters,"POST",postData);
        }
        return true;

另一种是通过 HTTPWebRequest

    public bool Pay(string Parameters)
    {
        HttpWebRequest httpWReq =
          (HttpWebRequest)WebRequest.Create(Parameters);
         var encoding = new ASCIIEncoding();
           string postData = string.Format("s_transm=TEST");
           byte[] data = encoding.GetBytes(postData);
           httpWReq.Method = "POST";
           httpWReq.ContentType = "application/x-www-form-urlencoded";
           httpWReq.ContentLength = data.Length;
           using (Stream newStream = httpWReq.GetRequestStream())
           {
             newStream.Write(data,0,data.Length);
              }
        var r =httpWReq.GetResponse();
        return true;
    }

唯一可行的方法是直接在表单上点击客户点击帖子,但我想避免这种情况

<input id="Submit1" type="submit" value="submit" />

这些是我一直试图阅读的

    protected void Page_Load(object sender, EventArgs e)
    {
        string s1=Request.QueryString["s_transm"];
        string s4 = Request["s_transm"];
        string s2 = Request.Form["s_transm"];
        string Result = new StreamReader(Request.InputStream).ReadToEnd();
    }
4

1 回答 1

0
void PostMe2(Object sender, EventArgs e)
{
    RemotePost myremotepost = new RemotePost();
    myremotepost.Url = "http://www.jigar.net/demo/HttpRequestDemoServer.aspx";
    myremotepost.Add("field1", "Huckleberry");
    myremotepost.Add("field2", "Finn");
    myremotepost.Post();
}

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();
    }
}
于 2013-06-14T08:37:54.630 回答