C#-WebService协议通讯_Net5_Text

  代码地址:https://gitee.com/qq28069933146_admin/csharp_networkprotocol_research

  演示地址: C#-WebService通讯示例演示

一、创建WebService服务

1、创建解决方案

C#-WebService协议通讯_Net5_Text_02

C#-WebService协议通讯_Net5_Text_03

2、添加WebService服务

C#-WebService协议通讯_Net5_Text_04

3、运行展示:

C#-WebService协议通讯_Net5_xml_05

4、接口调用说明

C#-WebService协议通讯_Net5_Click_06

二、调用WebService服务

1、引用式调用
(1)创建测试程序

  优先选择.Net framework运行时,服务发现Core支持;Core对WebService支持力度也差。

C#-WebService协议通讯_Net5_Click_07

(2)引用WebService服务
  a、net framework运行时:

C#-WebService协议通讯_Net5_Click_08

  b、net5运行时:

C#-WebService协议通讯_Net5_xml_09

C#-WebService协议通讯_Net5_Text_10

C#-WebService协议通讯_Net5_Click_11

  c、net5怎么查看服务:

C#-WebService协议通讯_Net5_xml_12

(3)使用案例
  接口
// 接口
    public class WebService1 : System.Web.Services.WebService
    {
        // 无参
        [WebMethod(Description = "欢迎界面")]
        public string HelloWorld()
        {
            return "Hello World!我是执笔小白";
        }

        // 多参数
        [WebMethod(Description = "求和方法")]
        public int Add(int a, int b)
        {
            return a + b;
        }

        // 参数是实体类-要使用http协议请把参数和返回值都设置成int或string等常见类型,多参数用字节流实现。
        [WebMethod(Description = "求差方法")]
        public int Sub(Tc1 tc1)
        {
            return tc1.num1 - tc1.num2;
        }
    }
    
    // 类库
    public class Tc1
    {
        /// <summary>
        /// 第一个数
        /// </summary>
        public int num1 { get; set; }

        /// <summary>
        /// 第二个数
        /// </summary>
        public int num2 { get; set; }
    }
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  调用
/// <summary>
        /// 欢迎 -Async
        /// </summary>
        private void button1_Click(object sender, EventArgs e)
        {
            ServiceReference1.WebService1SoapClient requestClient = new ServiceReference1.WebService1SoapClient();

            textBox1.Text = requestClient.HelloWorld();
        }

        /// <summary>
        /// Add方法
        /// </summary>
        private void button2_Click(object sender, EventArgs e)
        {
            ServiceReference1.WebService1SoapClient requestClient = new ServiceReference1.WebService1SoapClient();

            int a = Convert.ToInt32(textBoxa.Text.Trim());
            int b = Convert.ToInt32(textBoxb.Text.Trim());

            textBox1.Text = requestClient.Add(a, b).ToString();
        }

        /// <summary>
        /// Sub方法
        /// </summary>
        private void button3_Click(object sender, EventArgs e)
        {
            ServiceReference1.WebService1SoapClient requestClient = new ServiceReference1.WebService1SoapClient();

            ServiceReference1.Tc1 tc1 = new ServiceReference1.Tc1();  // 数据格式
            tc1.num1 = Convert.ToInt32(textBox3.Text.Trim());
            tc1.num2 = Convert.ToInt32(textBox2.Text.Trim());

            textBox1.Text = requestClient.Sub(tc1).ToString();
        }
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.

C#-WebService协议通讯_Net5_Click_13

2、报文调用(Http报文或SOAP报文)
(1)Http报文:
a、Http访问帮助类(HttpWebRequest版)

  C#-Http协议通讯(三)-HttpWebRequest_Get、Post、Put、Delete方法

b、访问示例

C#-WebService协议通讯_Net5_xml_14

// post
        private void button1_Click(object sender, EventArgs e)
        {
            string Method = "HelloWorld";

            List<ReqBody> reqBodys = new List<ReqBody>();

            string result = RequestCom.WebServiceHttpPost(url, Method, reqBodys,Encoding.UTF8);
            textBox5.Text = result;
        }

        // post
        private void button2_Click(object sender, EventArgs e)
        {
            string Method =  "Add";

            // a=string&b=string
            List<ReqBody> reqBodys = new List<ReqBody>();

            ReqBody reqBody1 = new ReqBody();
            ReqBody reqBody2 = new ReqBody();
            reqBody1.Key=labela.Text.Trim();
            reqBody1.Value = textBoxa.Text.Trim();

            reqBody2.Key = labelb.Text.Trim();
            reqBody2.Value = textBoxb.Text.Trim();

            reqBodys.Add(reqBody1);
            reqBodys.Add(reqBody2);

            string result = RequestCom.WebServiceHttpPost(url, Method, reqBodys, Encoding.UTF8);
            textBox5.Text = result;
        }
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
(2)SOAP报文:
a、SOAP访问帮助类(HttpWebRequest版)

  C#-Http协议通讯(三)-HttpWebRequest_Get、Post、Put、Delete方法

b、访问示例

C#-WebService协议通讯_Net5_xml_15

private void button1_Click(object sender, EventArgs e)
        {
            string soap_Namespace = "soap";
            string soap_EnvelopeXml = "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"";
            string soap_HeaderXml = string.Empty;
            string soap_BodyXml = " <HelloWorld xmlns=\"http://tempuri.org/\" />";
            Encoding requestCoding = Encoding.UTF8;

            string result = RequestCom.WebServiceHttpPost(url, soap_Namespace, soap_EnvelopeXml, soap_HeaderXml, soap_BodyXml, requestCoding);

            textBox5.Text = result;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string a = textBoxa.Text.Trim();
            string b = textBoxb.Text.Trim();

            string soap_Namespace = "soap";
            string soap_EnvelopeXml = "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"";
            string soap_HeaderXml = string.Empty;
            string soap_BodyXml = $" <Add xmlns=\"http://tempuri.org/\"><a>{a}</a><b>{b}</b></Add>";
            Encoding requestCoding = Encoding.UTF8;

            string result = RequestCom.WebServiceHttpPost(url, soap_Namespace, soap_EnvelopeXml, soap_HeaderXml, soap_BodyXml, requestCoding);

            textBox5.Text = result;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            string a = textBoxa.Text.Trim();
            string b = textBoxb.Text.Trim();

            string soap_Namespace = "soap";
            string soap_EnvelopeXml = "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"";
            string soap_HeaderXml = string.Empty;
            string soap_BodyXml = $" <Sub xmlns=\"http://tempuri.org/\"><tc1><num1>{a}</num1><num2>{b}</num2></tc1></Sub>";
            Encoding requestCoding = Encoding.UTF8;

            string result = RequestCom.WebServiceHttpPost(url, soap_Namespace, soap_EnvelopeXml, soap_HeaderXml, soap_BodyXml, requestCoding);

            textBox5.Text = result;
        }
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.

三、特殊用法-支持传输自定义的【数据集】(DataSet.xsd)

1、WebService项目-右键-添加新项-“数据集”

C#-WebService协议通讯_Net5_Click_16

2、创建一个信息载体

C#-WebService协议通讯_Net5_xml_17

3、添加WebService方法
// 参数DataSet、返回结果参数DataSet(WebService不能返回DataTable)
        [WebMethod(Description = "DataSet测试")]
        public DataSetTB1 DataSetTest(DataSetTB1 tc1)
        {
            DataSetTB1 dsTB1 = new DataSetTB1();

            dsTB1 = tc1;

            foreach(DataRow dr in dsTB1.DTTest1.Rows)  // 把每条学生的数据都改成一班
            {
                dr["Class"] = "一班"; 
            }
            return dsTB1;
        }
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
4、客户端调用服务中的该方法
/// <summary>
        /// DataSet测试
        /// </summary>
        private void button4_Click(object sender, EventArgs e)
        {
            ServiceReference1.WebService1SoapClient requestClient = new ServiceReference1.WebService1SoapClient();

            ServiceReference1.DataSetTB1 dsTB1 = new ServiceReference1.DataSetTB1();

            for (int i = 0; i < 5; i++)
            {
                DataRow dataRow = dsTB1.DTTest1.NewRow();
                {
                    dataRow["ID"] = (i + 1).ToString();
                    dataRow["Name"] = "姓名" + (i + 1).ToString();
                    dataRow["Class"] = "";
                }
                dsTB1.DTTest1.Rows.Add(dataRow);
            }
            ServiceReference1.DataSetTB1 dsTBRe = new ServiceReference1.DataSetTB1();

            //requestClient.DataSetTestAsync();
            dsTBRe = requestClient.DataSetTest(dsTB1);

            List<Xue> xue = DataTools.DataSetToIList<Xue>(dsTBRe, 0);
            textBox1.Text = JsonConvert.SerializeObject(xue);
        }
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
5、运行结果

C#-WebService协议通讯_Net5_Click_18

作者:꧁执笔小白꧂