HttpWebRequest传值

本文介绍了一个使用C#进行Web POST请求的例子。通过构造HTTP请求并传递用户名和密码参数到指定URL,演示了如何从Web服务器获取响应。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

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     }

 

转载于:https://www.cnblogs.com/yf2011/p/3989239.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值