返回值组合:
json返回
1 StringBuilder sb = new StringBuilder(); 2 sb.Append("{"); 3 sb.Append("\"Status\":\"" + 123 + "\""); 4 sb.Append("}");
json数组
StringBuilder sb = new StringBuilder(); sb.Append("["); for (int i = 0; i < 10; i++) { sb.Append("{"); sb.Append("\"name\":\"" + i + "\","); sb.Append("\"name1\":\"" + (i+1) + "\""); sb.Append("},"); } sb.Remove(sb.ToString().LastIndexOf(','), 1); sb.Append("]");
案例1:
1 public void GetClass(string name, string id) 2 { 3 try 4 { 5 string connectString = System.Configuration.ConfigurationSettings.AppSettings["connStr"]; 6 SqlConnection conn = new SqlConnection(connectString); 7 conn.Open(); 8 SqlCommand comm = new SqlCommand(); 9 comm.Connection = conn; 10 comm.CommandText = "PROC_ChoicePersonnel"; 11 comm.CommandType = System.Data.CommandType.StoredProcedure; 12 //传值以及赋值 13 SqlParameter[] sps = new SqlParameter[] { 14 new SqlParameter("@ClassName",name), 15 new SqlParameter("@id",id), 16 new SqlParameter("@flag","class") 17 }; 18 comm.Parameters.AddRange(sps); 19 20 SqlDataAdapter sdap = new SqlDataAdapter(); 21 sdap.SelectCommand = comm; 22 DataTable dt = new DataTable(); 23 sdap.Fill(dt); 24 conn.Close(); 25 StringBuilder sb = new StringBuilder(); 26 sb.Append("["); 27 for (int i = 0; i < dt.Rows.Count; i++) 28 { 29 sb.Append("{"); 30 sb.Append("\"id\":\"" + dt.Rows[i]["id"].ToString() + "\","); 31 sb.Append("\"name\":\"" + dt.Rows[i]["name"].ToString() + "\""); 32 sb.Append("},"); 33 } 34 sb.Remove(sb.ToString().LastIndexOf(','), 1); 35 sb.Append("]"); 36 Result(sb); 37 } 38 catch (Exception ex) 39 { 40 41 } 42 } 43 44 private void Result(StringBuilder sb) 45 { 46 Context.Response.Charset = "UTF-8"; //设置字符集类型 47 Context.Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8"); 48 Context.Response.ContentType = "text/plain"; 49 Context.Response.Write(sb.ToString()); 50 Context.Response.End(); 51 }
C#后台调用HTTP外网接口
1.get方法调用接口获取json文件内容
1 public void GetFunction() 2 { 3 4 string serviceAddress = "http://...?aaa=111"; 5 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(serviceAddress); 6 request.Method = "GET"; 7 request.ContentType = "text/html;charset=UTF-8"; 8 HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 9 Stream myResponseStream = response.GetResponseStream(); 10 StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8); 11 string retString = myStreamReader.ReadToEnd(); 12 myStreamReader.Close(); 13 myResponseStream.Close(); 14 Response.Write(retString); 15 }
2.post方法调用接口获取json文件内容
1 public void PostFunction() 2 { 3 4 string serviceAddress = "http://..."; 5 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(serviceAddress); 6 7 request.Method = "POST"; 8 request.ContentType = "application/json"; 9 string strContent = @"{ ""a"": ""1"",""b"": ""2"",""c"": ""3""}"; 10 using (StreamWriter dataStream = new StreamWriter(request.GetRequestStream())) 11 { 12 dataStream.Write(strContent); 13 dataStream.Close(); 14 } 15 HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 16 string encoding = response.ContentEncoding; 17 if (encoding == null || encoding.Length < 1) 18 { 19 encoding = "UTF-8"; //默认编码 20 } 21 StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(encoding)); 22 string retString = reader.ReadToEnd(); 23 //解析josn 24 JObject jo = JObject.Parse(retString); 25 Response.Write(jo["message"]["mmmm"].ToString()); 26 27 }
3.
1 [WebMethod] 2 public void tt() 3 { 4 //地址 5 string url = "http://..."; 6 string a = "1"; 7 string b = "2"; 8 string c = "15706421215"; 9 string d = "5"; 10 //传的参数 11 //string datastr1 = "id=" + System.Web.HttpUtility.UrlEncode(ids); 12 string datastr1 = "a=" + a + "&b=" + b + "&c=" + c + "&d=" + d+ "&e="; 13 //转成字节 14 byte[] bytearray1 = Encoding.UTF8.GetBytes(datastr1); 15 //创建WebRequest 16 HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(url); 17 //POST方式 18 webrequest.Method = "POST"; 19 // <form encType=””>中默认的encType,form表单数据被编码为key/value格式发送到服务器(表单默认的提交数据的格式) 20 webrequest.ContentType = "application/x-www-form-urlencoded"; 21 //获取字节数 22 webrequest.ContentLength = Encoding.UTF8.GetByteCount(datastr1); 23 //获取用于写入请求数据的 System.IO.Stream 对象 24 Stream webstream = webrequest.GetRequestStream(); 25 //向当前流中写入字节序列,并将此流中的当前位置提升写入的字节数。 26 webstream.Write(bytearray1, 0, bytearray1.Length); 27 //获取返回数据 28 HttpWebResponse response = (HttpWebResponse)webrequest.GetResponse(); 29 //转码 30 StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8); 31 //返回的结果 32 string ret = sr.ReadToEnd(); 33 }