赞
踩
最近写了不少api接口调用,移动端。pc端,目前感觉区别不大,唯一感到不一样的地方就是移动端因为不能操作太多逻辑,所以接口返回的数据格式比pc端详细的多,有可能本来在pc端嵌套一次json,如果放在移动端你就要去嵌套两次或者3次。
对于json参数这里特别说明一下, 一种是string类型,一种是json类型
string类型不用多说,最常见,调用方式直接拷贝下面代码:
StringBuilder sb = new StringBuilder();
sb.Append("mobile=" + mobile);
string posturl = Class2.Url_banmaService + "/WebService.asmx/sendmsg_yy";
//string posturl = "http://localhost:20227/BanMaTeacher/WebService.asmx/sendmsg_yy";
WebRequest mywebRequest = null;
mywebRequest = WebRequest.Create(posturl);
mywebRequest.Method = "POST";
Encoding enc = Encoding.GetEncoding("utf-8");
byte[] byte1 = enc.GetBytes(sb.ToString());
mywebRequest.ContentType = "application/x-www-form-urlencoded;charset=utf-8";
mywebRequest.ContentLength = byte1.Length;
Stream sw = null;
sw = mywebRequest.GetRequestStream();
sw.Write(byte1, 0, byte1.Length);
HttpWebResponse myResp = (HttpWebResponse)mywebRequest.GetResponse();
StreamReader myStream = new StreamReader(myResp.GetResponseStream(), enc);
string re = myStream.ReadToEnd();
注意一般到这里基本就完事了,下面是处理调用接口里面的数据的,下面的是直接一个json对象格式,还有一种是json数组,把Jobject再转成JArray就可以了
JObject jo = (JObject)JsonConvert.DeserializeObject(re);
string zone = jo["data"].ToString();
string mssg = jo["error_message"].ToString();
好了下面是另外一种参数格式的接口,参数是json类型的
System.Text.StringBuilder sb = new System.Text.StringBuilder();
WebRequest myHttpWebRequest = null;
sb.Append("{\"apiKey\":\"MER20160719959\"}");
string posturl = "http://www.51bangxue.cn:8088/qbaymer/api/getToken";
//string posturl = Class2.Url_banmaService + "/WebService.asmx/CreateStudent";
myHttpWebRequest = WebRequest.Create(posturl);
myHttpWebRequest.Method = "POST";
Encoding enc = Encoding.GetEncoding("utf-8");
byte[] byte1 = enc.GetBytes(sb.ToString());
myHttpWebRequest.ContentType = "application/json;charset=utf-8";
myHttpWebRequest.ContentLength = byte1.Length;
Stream sw = null;
sw = myHttpWebRequest.GetRequestStream();//获取用于写入请求数据的Stream对象
sw.Write(byte1, 0, byte1.Length);
HttpWebResponse myResp = (HttpWebResponse)myHttpWebRequest.GetResponse();
StreamReader myStream = new StreamReader(myResp.GetResponseStream(), enc);
string re = myStream.ReadToEnd();
JObject jo = (JObject)JsonConvert.DeserializeObject(re);
string token = jo["token"].ToString();
string returnCode = jo["returnCode"].ToString();
if (returnCode!="00")
{
context.Response.Write("信用卡接口异常");
return;
}
OK以上就是我要说的两种参数格式调用接口的方法,区别不大,就是用stringbuilder拼接参数的时候格式不一样,还有就是ContentType不一样,其他都一样。
落下了一种,今天补上:
GET形式的接口,以流读取
System.Text.StringBuilder sbend = new StringBuilder();
WebRequest myRequestEnd = null;
sbend.Append("subscibeid=" + tokent);
sbend.Append("token=" + token);
sbend.Append("useruuid=" + ts + dt_umobile.Rows[0]["id"].ToString());
string posturlend = "http://www.51bangxue.cn:8088/qbaymer/api/apiProcess?subscibeid=" + tokent + "&token=" + token + "&useruuid=" + ts + dt_umobile.Rows[0]["id"].ToString();
myRequestEnd = WebRequest.Create(posturlend);
myRequestEnd.Method = "GET";
HttpWebResponse myresponse = (HttpWebResponse)myRequestEnd.GetResponse();
StreamReader srd = new StreamReader(myresponse.GetResponseStream(), enct);
string reend = srd.ReadToEnd();
//JObject joend = (JObject)JsonConvert.DeserializeObject(reend);
context.Response.Write(reend);
直接拼接参数,不用stringbuilder,Httpwebstream也不用了。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。