标签:bytes coding end cat login post 姓名 客户 type
服务方法
1 [AllowAnonymousAttribute] 2 [HttpPost] 3 public string PostWebName(dynamic login) 4 { 5 Dictionary<string, string> dict = new Dictionary<string, string>(); 6 dict.Add("姓名", Convert.ToString(login.name)); 7 dict.Add("密码", Convert.ToString(login.pwd)); 8 9 return Tools.ConvertToJsonStr(dict); 10 11 }
客户端调用方法
1 Dictionary<string, string> dic = new Dictionary<string, string>(); 2 dic.Add("name", "第一个参数"); 3 dic.Add("pwd", "第二个参数"); 4 5 string url = "http://localhost:10450/api/EmpInfo/PostWebName"; 6 7 textBox2.Text = HttpPost(url, ObjectToJson(dic));
1 string HttpPost(string URL, string Para) 2 { 3 // 创建HttpWebRequest对象 4 HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(URL); 5 httpRequest.Method = "POST"; 6 httpRequest.ContentType = "application/json"; 7 httpRequest.Headers.Add("Authorization", "lzsin"); 8 9 byte[] bytes = Encoding.UTF8.GetBytes(Para); 10 using (Stream reqStream = httpRequest.GetRequestStream()) 11 { 12 reqStream.Write(bytes, 0, bytes.Length); 13 reqStream.Flush(); 14 } 15 try 16 { 17 using (HttpWebResponse myResponse = (HttpWebResponse)httpRequest.GetResponse()) 18 { 19 StreamReader sr = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8); 20 string responseString = sr.ReadToEnd(); 21 return responseString; 22 } 23 } 24 catch (WebException ex) 25 { 26 var res = (HttpWebResponse)ex.Response; 27 StreamReader sr = new StreamReader(res.GetResponseStream(), Encoding.UTF8); 28 string str = sr.ReadToEnd(); 29 return str; 30 } 31 }
标签:bytes coding end cat login post 姓名 客户 type
原文地址:https://www.cnblogs.com/lzsin/p/12601738.html