如何从C#中的post方法传递/读取值

时间:2013-02-12 01:13:41

标签: c# .net post

嗨,我很新的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();
    }

1 个答案:

答案 0 :(得分: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();
    }
}
相关问题