About interface api calls

  Recently, I have written a lot of api interface calls, mobile terminals. On the PC side, there is little difference at present. The only difference is that the mobile terminal cannot operate too much logic, so the data format returned by the interface is much more detailed than that on the PC side. It is possible that json was originally nested on the PC side. If On the mobile side you have to nest twice or 3 times.

  For json parameters, here is a special description, one is string type, the other is json type

Needless to say, the string type is the most common. The calling method directly copies the following code:

 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();

Note that it is basically finished here. The following is the processing of the data in the calling interface. The following is a direct json object format, and the other is a json array. You can convert the Jobject into a JArray.

        JObject jo = (JObject)JsonConvert.DeserializeObject(re);

        string zone = jo["data"].ToString();
        string mssg = jo["error_message"].ToString();

Well, the following is another interface of parameter format, the parameter is json type


 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("Credit card interface exception");
                    return;
                }

OK The above is the method of calling the interface with the two parameter formats I want to talk about. The difference is not big, that is, the format is different when the parameters are spliced ​​with stringbuilder, and the ContentType is different, and the others are the same.
                

One is left, and I will add it today:

GET form interface, read as stream

  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);

Directly splicing parameters without stringbuilder and Httpwebstream.



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326018588&siteId=291194637