HttpWebRequest传值

 

From:发送方

 1 class Program
 2     {
 3         static void Main(string[] args)
 4         {
 5             string strId = "zhangsan";
 6             string strPassword = "123";
 7             string str = "userid=" + strId;
 8             str += "&password=" + strPassword;
 9             string url = "http://localhost:7392/WebForm1.aspx";
10             byte[] bs = Encoding.ASCII.GetBytes(str);
11             HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
12             req.Method = "POST";
13             req.ContentType = "application/x-www-form-urlencoded";
14             req.ContentLength = bs.Length;
15             req.Timeout = 120000;
16             try
17             {
18                 using (System.IO.Stream reqStream = req.GetRequestStream())
19                 {
20                     reqStream.Write(bs, 0, bs.Length);
21                     reqStream.Close();
22                     reqStream.Dispose();
23                 }
24                 using (WebResponse wr = req.GetResponse()) //返回
25                 {
26                     System.IO.Stream res = wr.GetResponseStream();
27                     System.IO.StreamReader reader = new System.IO.StreamReader(res);
28                     string ResStr = reader.ReadToEnd(); //返回结果
29                     wr.Close();
30                     Console.WriteLine(ResStr); ;
31                 }
32             }
33             catch (Exception ex)
34             {
35                 Console.WriteLine(ex.Message);
36             }
37             Console.ReadLine();
38         }
39     }
40 }

 

To:接收方

 

1  public partial class WebForm1 : System.Web.UI.Page
2     {
3         protected void Page_Load(object sender, EventArgs e)
4         {
5             string ID = Request.Form["userid"].ToString();
6             string password = Request.Form["password"].ToString();
7         }
8     }

 

posted @ 2014-09-23 20:31  J.Y  阅读(1850)  评论(0编辑  收藏  举报