发送json数据
1 /// <summary> 2 /// 传输Json并获取url的返回值 3 /// </summary> 4 public static string RequestJson(string url, string data)//发送Json包 5 { 6 string strURL = url; 7 System.Net.HttpWebRequest request; 8 request = (System.Net.HttpWebRequest)WebRequest.Create(strURL); 9 request.Method = "POST"; 10 request.ContentType = "application/json;charset=UTF-8"; 11 string paraUrlCoded = data; 12 byte[] payload; 13 payload = System.Text.Encoding.UTF8.GetBytes(paraUrlCoded); 14 request.ContentLength = payload.Length; 15 Stream writer = request.GetRequestStream(); 16 writer.Write(payload, 0, payload.Length); 17 writer.Close(); 18 System.Net.HttpWebResponse response; 19 response = (System.Net.HttpWebResponse)request.GetResponse(); 20 System.IO.Stream s; 21 s = response.GetResponseStream(); 22 string StrDate = ""; 23 string strValue = ""; 24 StreamReader Reader = new StreamReader(s, Encoding.UTF8); 25 while ((StrDate = Reader.ReadLine()) != null) 26 { 27 strValue += StrDate + " "; 28 } 29 return strValue; 30 }