.NET Core 3.1调用.NET Framework的WebService

1.0准备一个简单的WebService

 

在HelloWebService添加两个方法

namespace WebServiceTest

{

    /// <summary>

    /// HelloWebService 的摘要说明

    /// </summary>

    [WebService(Namespace = "http://tempuri.org/")]

    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

    [System.ComponentModel.ToolboxItem(false)]

    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消注释以下行。

    //[System.Web.Script.Services.ScriptService]

    public class HelloWebService : System.Web.Services.WebService

    {

        public Authentication AuthenticationHeader;

 

        [WebMethod]

        public string HelloWorld()

        {

            return "Hello World " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");

        }

        [WebMethod, SoapHeader("AuthenticationHeader")]

        public string HelloWorldHasParam(string paramStr, Person person, Authentication authentication)

        {

            AuthenticationHeader = authentication;

            Console.WriteLine($"{paramStr} ; person.Name={person.Name} ; person.Age={person.Age} ; authentication = {authentication.ServiceUser}");

            return $"{paramStr} ; person.Name={person.Name} ; person.Age={person.Age} ; authentication = {authentication.ServiceUser}"; ;

        }

    }

    public class Person

    {

        public string Name { get; set; }

        public int Age { get; set; }

    }

    public class Authentication : SoapHeader

    {

        public string ServiceUser;

        public string ServicePassword;

        public string System;

        public string Token;

        public string DeviceID;

    }

}

 

2.0.NET Core 调用Webservice

2.1.方法1:通过添加引用服务调用WebService

新建一个.NET Core 3.1的web项目,然后添加添加服务,如下图的步骤

 

 

 

 

 

 

 

BasicHttpBinding binding = new BasicHttpBinding();

EndpointAddress address = new EndpointAddress("http://127.0.0.1:8999/HelloWebService.asmx");

HelloWebServiceSoapClient soapClient = new HelloWebServiceSoapClient(binding, address);

var helloWorldRespone = soapClient.HelloWorldAsync();

string result1 = helloWorldRespone.Result;

 

2.2.方法2通过WebClient对象post XML 文档到服务器调用 WebService ,XMl求参考请求和响应示例。

 

 

 

string xmlStr =

    @"<soap12:Envelope xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:soap12=""http://www.w3.org/2003/05/soap-envelope"">

            <soap12:Header>

            <Authentication xmlns=""http://tempuri.org/"">

                <ServiceUser>张三</ServiceUser>

                <ServicePassword>ServicePassword123</ServicePassword>

                <System>System456</System>

                <Token>Token1234</Token>

                <DeviceID>DeviceIDsss</DeviceID>

            </Authentication>

            </soap12:Header>

            <soap12:Body>

            <HelloWorldHasParam xmlns=""http://tempuri.org/"">

                <paramStr>first param</paramStr>

                <person>

                <Name>李四</Name>

                <Age>18</Age>

                </person>

                <authentication>

                <ServiceUser>张三</ServiceUser>

                <ServicePassword>ServicePassword123</ServicePassword>

                <System>System456</System>

                <Token>Token1234</Token>

                <DeviceID>DeviceIDsss</DeviceID>

                </authentication>

            </HelloWorldHasParam>

            </soap12:Body>

        </soap12:Envelope>";

using (WebClient webClient = new WebClient())

{

    webClient.Proxy = null;

    byte[] postDatabyte = Encoding.GetEncoding("UTF-8").GetBytes(xmlStr);

    webClient.Headers.Add("Content-Type", "application/soap+xml; charset=utf-8");

    //webClient.Headers.Add("Content-Type", "text/xml; charset=utf-8");

    byte[] responseData = webClient.UploadData("http://127.0.0.1:8999/HelloWebService.asmx", "POST", postDatabyte);

    //解码

    string responsestring = Encoding.GetEncoding("UTF-8").GetString(responseData);

    XmlDocument xdoc = new XmlDocument();

    xdoc.LoadXml(responsestring);

    //解析返回值

    var nodeValue = xdoc.GetElementsByTagName("HelloWorldHasParamResult")[0].InnerXml;

    Console.WriteLine(responsestring);

}

2.3.方法3通过HttpClient对象post XML 文档到服务器调用 WebService

首先在Startup.cs类 中注入HttpClientFactory.

string xmlStr =

    @"<soap12:Envelope xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:soap12=""http://www.w3.org/2003/05/soap-envelope"">

            <soap12:Header>

            <Authentication xmlns=""http://tempuri.org/"">

                <ServiceUser>张三</ServiceUser>

                <ServicePassword>ServicePassword123</ServicePassword>

                <System>System456</System>

                <Token>Token1234</Token>

                <DeviceID>DeviceIDsss</DeviceID>

            </Authentication>

            </soap12:Header>

            <soap12:Body>

            <HelloWorldHasParam xmlns=""http://tempuri.org/"">

                <paramStr>first param</paramStr>

                <person>

                <Name>李四</Name>

                <Age>18</Age>

                </person>

                <authentication>

                <ServiceUser>张三</ServiceUser>

                <ServicePassword>ServicePassword123</ServicePassword>

                <System>System456</System>

                <Token>Token1234</Token>

                <DeviceID>DeviceIDsss</DeviceID>

                </authentication>

            </HelloWorldHasParam>

            </soap12:Body>

        </soap12:Envelope>";

HttpContent contentPost3 = new ByteArrayContent(Encoding.GetEncoding("UTF-8").GetBytes(xmlStr));

var contentType = new MediaTypeWithQualityHeaderValue("text/xml");

contentType.CharSet = "utf-8";

contentPost3.Headers.ContentType = contentType;

using (var client = _httpClientFactory.CreateClient())

{

    var op = await client.PostAsync("http://127.0.0.1:8999/HelloWebService.asmx", contentPost3);

    //获取到返回结果的XML

    string result = Encoding.GetEncoding("UTF-8").GetString(op.Content.ReadAsByteArrayAsync().Result);

    Console.WriteLine(result);

}

 

返回结果:

<?xml version="1.0" encoding="utf-8"?>

<soap:Envelope

    xmlns:soap="http://www.w3.org/2003/05/soap-envelope"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xmlns:xsd="http://www.w3.org/2001/XMLSchema">

    <soap:Body>

        <HelloWorldHasParamResponse xmlns="http://tempuri.org/">

            <HelloWorldHasParamResult>first param ; person.Name=李四 ; person.Age=18 ; authentication = 张三</HelloWorldHasParamResult>

        </HelloWorldHasParamResponse>

    </soap:Body>

</soap:Envelope>