.net发送post请求的两种方法
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); } }
声明:该文观点仅代表作者本人,入门客AI创业平台信息发布平台仅提供信息存储空间服务,如有疑问请联系rumenke@qq.com。