【C#】调用 webservice 的三种方法总结


Web Service也称为web服务,它是一种跨编程语言和操作系统平台的远程调用技术。Web Service采用标准的SOAP协议传输(SOAP:Simple Object Access Protocol简单对象访问协议,soap属于w3c标准。并且soap协议是基于http的应用层协议传输xml数据)。Web Service采用WSDL作为描述语言,也就是Web Service 的使用说明书。并且W3C为Web Service制定了一套传输数据类型,使用xml进行描述,即XSD(XML Schema Datatypes),任何语言写的web Service 接口在发送数据的时候都要转换成WebService标准的XSD发送。

【1】直接添加服务引用,然后调用相关方法。

【2】使用 HttpPost 请求调用 WebService

public class WebServiceDemo : System.Web.Services.WebService
    {
    
    

        [WebMethod]
        public string HelloWorld()
        {
    
    
            return "Hello World";
        }

        [WebMethod]
        public string Sum(string param1, string param2)
        {
    
    
            int num1 = Convert.ToInt32(param1);
            int num2 = Convert.ToInt32(param2);

            int sum = num1 + num2;

            return sum.ToString();
        }
    }

客户端调用代码:

 class Program
    {
    
    
        static void Main(string[] args)
        {
    
    
            Program program = new Program();
            string url = "http://localhost:12544/WebServiceDemo.asmx";
            string method = "Sum";
            string num1 = "1";
            string num2 = "2";

            string result = program.HttpPostWebService(url, method, num1, num2);

            Console.WriteLine(result);
            Console.ReadKey();
        }

        public string HttpPostWebService(string url,string method,string num1,string num2)
        {
    
    
            string result = string.Empty;
            string param = string.Empty;
            byte[] bytes = null;
            Stream writer = null;
            HttpWebRequest request = null;
            HttpWebResponse response = null;

            param = HttpUtility.UrlEncode("x") + "=" + HttpUtility.UrlEncode(num1) + "&" + HttpUtility.UrlEncode("y") + "=" + HttpUtility.UrlEncode(num2);
            bytes = Encoding.UTF8.GetBytes(param);

            request = (HttpWebRequest)WebRequest.Create(url + "/" + method);
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = bytes.Length;

            try
            {
    
    
                writer = request.GetRequestStream();        //获取用于写入请求数据的Stream对象
            }
            catch (Exception ex)
            {
    
    
                return "";
            }

            writer.Write(bytes, 0, bytes.Length);       //把参数数据写入请求数据流
            writer.Close();

            try
            {
    
    
                response = (HttpWebResponse)request.GetResponse();      //获得响应
            }
            catch (WebException ex)
            {
    
    
                return "";
            }

            #region 这种方式读取到的是一个返回的结果字符串
            Stream stream = response.GetResponseStream();        //获取响应流
            XmlTextReader Reader = new XmlTextReader(stream);
            Reader.MoveToContent();
            result = Reader.ReadInnerXml();
            #endregion

            #region 这种方式读取到的是一个Xml格式的字符串
            //StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
            //result = reader.ReadToEnd();
            #endregion 

            response.Dispose();
            response.Close();

            //reader.Close();
            //reader.Dispose();

            Reader.Dispose();
            Reader.Close();

            stream.Dispose();
            stream.Close();

            return result;
        }
    }

用 PostMan 工具测试:
在这里插入图片描述

注意:webservice 中的方法参数变量的名字和你提供的变量名字一样;param = HttpUtility.UrlEncode
(“x”) + “=” + HttpUtility.UrlEncode (num1) + “&” +
HttpUtility.UrlEncode (“y”) + “=” + HttpUtility.UrlEncode (num2);

【3】动态调用 WebService

动态调用Webservice

如果不是为了维护老项目 ,grpc了解一下

GRPC是一个高性能、通用的开源RPC框架,基于底层HTTP/2协议标准和协议层Protobuf序列化协议开发,支持众多的开发语言。

gRPC 也是基于以下理念:定义一个服务,指定其能够被远程调用的方法(包含参数和返回类型)。在服务端实现这个接口,并运行一个 gRPC服务器来处理客户端调用。在客户端拥有一个存根能够像服务端一样的方法。

gRPC使用protocol buffers作为接口描述语言(IDL)以及底层的信息交换格式

引用

C# 调用 webservice 的三种方法总结
gRPC简介
WebService全面详解

猜你喜欢

转载自blog.csdn.net/weixin_44231544/article/details/124922474