一、无法直接通过接口获取数据,需要转接口获取数据



ContentType :application/x-www-form-urlencoded:输入值为字符串格式
ContentType:application/json:输入值为json格式


第1种:输入字典类型转json类型:


后台访问接口_json后台访问接口_json格式_02


1 字典:Dictionary<string, object> parameters =new Dictionary<string, object>();
2 parameters.add(“key”,”value”);
3 string mm = GetPage("http://21.254.198.70:803/api/healthLogin", parameters);
4 /// <summary>
5 /// 指定Post地址
6 /// </summary>
7 /// <param name="url">请求后台地址</param>
8 /// <returns></returns>
9 public static string Post(string url, Dictionary<string, object> parameters)
10 {
11 try
12 {
13 string jsonString = JsonConvert.SerializeObject(parameters);
14 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
15 request.Method = "POST";
16 request.ContentType = "application/json";
17 using (StreamWriter dataStream = new StreamWriter(request.GetRequestStream()))
18 {
19 dataStream.Write(jsonString);
20 dataStream.Close();
21 }
22 HttpWebResponse response = (HttpWebResponse)request.GetResponse();
23 string encoding = response.ContentEncoding;
24 if (encoding == null || encoding.Length < 1)
25 {
26 encoding = "UTF-8"; //默认编码
27 }
28 StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(encoding));
29 var retString = reader.ReadToEnd();
30 return Regex.Unescape(retString); //处理返回的字符比如unicode转为中文
31 }
32 catch (Exception ex)
33 {
34 throw ex;
35 }
36 }

View Code

第2种:输入字符串类型转json类型:


后台访问接口_json后台访问接口_json格式_02


1 string mm = GetPage("http://21.254.198.70:803/api/healthLogin", "{\"phone\":\"" + phone + "\",\"code\":\"" + code + "\"}");
2
3 public string GetPage(string posturl, string postData)
4 {
5 Stream outstream = null;
6 Stream instream = null;
7 StreamReader sr = null;
8 HttpWebResponse response = null;
9 HttpWebRequest request = null;
10 Encoding encoding = Encoding.UTF8;
11 byte[] data = encoding.GetBytes(postData);
12 try
13 {
14 request = WebRequest.Create(posturl) as HttpWebRequest;
15 CookieContainer cookieContainer = new CookieContainer();
16 request.CookieContainer = cookieContainer;
17 request.AllowAutoRedirect = true;
18 request.Method = "POST";
19 request.ContentType = "application/json";
20 request.ContentLength = data.Length;
21 outstream = request.GetRequestStream();
22 outstream.Write(data, 0, data.Length);
23 outstream.Close();
24 response = request.GetResponse() as HttpWebResponse;
25 instream = response.GetResponseStream();
26 sr = new StreamReader(instream, encoding);
27 string content = sr.ReadToEnd();
28 string err = string.Empty;
29 return content;
30 }
31 catch (Exception ex)
32 {
33 string err = ex.Message;
34 return string.Empty;
35 }
36 }

View Code

二、直接过去数据



[HttpPost]
public string data(string url, object vo) { //传入接口连接和对象类型的参数
string request = JsonConvert.SerializeObject(vo);//将对象参数转化为json格式
IDictionary<string, string> keyValuePairs = new Dictionary<string, string>();
keyValuePairs.Add("stbid", "admin2");
keyValuePairs.Add("token", "9e72b676c19b455fadc5e4b484dbc9ba");//定义两个参数,给两个参数赋值
var data = Utils.HttpUtil.GetResponseData(url, request, "POST", "application/json", null, keyValuePairs);
return data;
}