码迷,mamicode.com
首页 > 其他好文 > 详细

ABP 后台调用接口 获取返回的数据

时间:2019-12-02 19:20:05      阅读:520      评论:0      收藏:0      [点我收藏+]

标签:png   service   pos   reader   get   code   turn   coding   response   

原文:https://www.cnblogs.com/i3yuan/p/10703500.html

 

 

insert 简单测试:

 

技术图片

 

 

        public void test8()
        {
            string url = "http://localhost:21021/api/services/app/Role/Create";

            var str2 = HttpPost3(url, System.IO.File.ReadAllText("2.txt"));
            Console.WriteLine(str2);
            Console.Read();
        }

 

        public string HttpPost4(string url, string body)
        {
            Encoding encoding = Encoding.UTF8;
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = "POST";
            request.ContentType = "application/json-patch+json";          
            byte[] buffer = encoding.GetBytes(body);
            request.ContentLength = buffer.Length;
            request.GetRequestStream().Write(buffer, 0, buffer.Length);
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
            {
                return reader.ReadToEnd();
            }
        }

 

 

 

 

update简单测试:

 

        public void test10()
        {
            string url = "http://localhost:21021/api/services/app/Menu/Update";
            var str2 = HttpPost5(url, "{\"name\": \"1\",\"url\": \"1\",\"id\": \"1\"}");
            Console.WriteLine(str2);
            Console.Read();
        }

 

        public string HttpPost5(string url, string body)
        {
            Encoding encoding = Encoding.UTF8;
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = "PUT";
            request.ContentType = "application/json-patch+json";
            byte[] buffer = encoding.GetBytes(body);
            request.ContentLength = buffer.Length;
            request.GetRequestStream().Write(buffer, 0, buffer.Length);
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
            {
                return reader.ReadToEnd();
            }
        }

 

 

delete简单测试:

        public void test11()
        {
            string url = "http://localhost:21021/api/services/app/Menu/Delete?Id=12";
            var str2 = Delete("", url);
            Console.WriteLine(str2);
            Console.Read();
        }

 

        public string Delete(string data, string uri)
        {
            HttpWebRequest Request = (HttpWebRequest)WebRequest.Create(uri);
            byte[] buf = System.Text.Encoding.GetEncoding("UTF-8").GetBytes(data);
            Request.Method = "DELETE";
            Request.ContentLength = buf.Length;
            Request.ContentType = "application/json";
            Request.MaximumAutomaticRedirections = 1;
            Request.AllowAutoRedirect = true;
            Stream stream = Request.GetRequestStream();
            stream.Write(buf, 0, buf.Length);
            stream.Close();
            HttpWebResponse Response = (HttpWebResponse)Request.GetResponse();
            StreamReader reader = new StreamReader(Response.GetResponseStream(), Encoding.UTF8);
            string returnXml = reader.ReadToEnd();//利用StreamReader就可以从响应内容从头读到尾
            reader.Close();
            Response.Close();
            return returnXml;
        }

 

ABP 后台调用接口 获取返回的数据

标签:png   service   pos   reader   get   code   turn   coding   response   

原文地址:https://www.cnblogs.com/guxingy/p/11972121.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!