public void HttpTestMe()
{
string url7 = "http://localhost:2810/Login/login";
WebRequest request7 = WebRequest.Create(url7);
request7.Method = "POST";
//post传参数
string postdata = "LoginName=365admin&Password=fob123";
byte[] bytes = Encoding.ASCII.GetBytes(postdata);
request7.ContentType = "application/x-www-form-urlencoded";
request7.ContentLength = postdata.Length;
Stream sendStream = request7.GetRequestStream();
sendStream.Write(bytes, 0, bytes.Length);
sendStream.Close();
//得到返回值
WebResponse response7 = request7.GetResponse();
string OrderQuantity = new StreamReader(response7.GetResponseStream(), Encoding.GetEncoding("gb2312")).ReadToEnd();
//转化成json对象处理
//List<GetOrderQuantity> getOrderQuantity = sr.Deserialize<List<GetOrderQuantity>>(OrderQuantity);
//方法二:需要添加using System.Net.Http;的引用
using (HttpClient client = new HttpClient())
{
client.BaseAddress = new Uri("http://localhost:2810");
var content = new FormUrlEncodedContent(new[] { new KeyValuePair<string, string>("LoginName", "365admin"), new KeyValuePair<string, string>("Password", "fob123")});
var result = client.PostAsync("/Login/login", content).Result;
string resultContent = result.Content.ReadAsStringAsync().Result;
Console.WriteLine(resultContent);
}
}.net发送post请求的两种方法
最新推荐文章于 2025-05-11 12:01:39 发布
本文介绍了一个使用C#进行HTTP POST请求的例子,演示了如何通过WebRequest和HttpClient两种方式发送带参数的POST请求,并接收服务器返回的数据。
3968

被折叠的 条评论
为什么被折叠?



