各位大虾   ASP.NET如何POST提交数据呢?  例如 要将a=1&b=2数据提交给https://www.test/forum/aaaa.aspx这个地址,并且打开这个页面,并且打开这个页面,谢谢

解决方案 »

  1.   

    httpwebrequest通过 post传值到b中的相关接口页面

    byte[] bdata = Encoding.Default.GetBytes(postData);   
      System.Net.HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(loginUrl);   
      myRequest.Method = "POST";   
      myRequest.ContentType = "application/x-www-form-urlencoded";   
      myRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";   
      myRequest.ContentLength = bdata.Length;   
      Stream newStream = myRequest.GetRequestStream();   
      newStream.Write(l_data, 0, bdata.Length);   
      newStream.Close();   
      HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();   
      StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.GetEncoding("GB2312"));   
      string content = reader.ReadToEnd();   
    注意编码form ACTION=""
      

  2.   

    我的代码是这样的
       private string RequestContent(string url,string conts)
        {
            string content = string.Empty;
            try
            {
                byte[] bdata = Encoding.Default.GetBytes(conts);
                System.Net.HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);
                myRequest.Method = "POST";
                myRequest.ContentType = "application/x-www-form-urlencoded";
                myRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
                myRequest.ContentLength = bdata.Length;
                Stream newStream = myRequest.GetRequestStream();
                newStream.Write(bdata, 0, bdata.Length);
                newStream.Close();
                HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
                StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.GetEncoding("GB2312"));
                content = reader.ReadToEnd();
            }
            catch
            {
                content = "Runtime Error";
            }
            return content;
        }
    url 是制定的地址 conts是需要传递的参数 如MERCHANTID=105411248160006&POSID=768362719&BRANCHID=410000000&ORDERID=123456577879879&PAYMENT=12&CURCODE=01&TXCODE=520100&REMARK1=&REMARK2=&MAC=398abb3bc800adfe605cab8e33468693 
    二楼  你的代码好像有问题 l_data 是不是 bdata  好像是定义错了吧! 
    我改了后  还是不行!  代码提示错误 无法连接到服务器! 
      

  3.   

    单独是否能访问
    string postData = "user=" + strUser;
    byte[] byteArray = Encoding.UTF8.GetBytes(postData); 
    ..
    newStream.Write(byteArray, 0, byteArray.Length);    
    newStream.Close();
    Visual Sniffer 抓包看看