- 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請求的兩種方法
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.