码迷,mamicode.com
首页 > Web开发 > 详细

HttpWebRequest传值

时间:2014-09-23 22:34:05      阅读:331      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   io   os   ar   for   div   

 

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     }

 

HttpWebRequest传值

标签:style   blog   http   color   io   os   ar   for   div   

原文地址:http://www.cnblogs.com/yf2011/p/3989239.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!