.net5调用WebService简单事例

1. 创建 .net5控制台项目:

dotnet new console -o WebServiceConsole

2. 添加全局工具

dotnet tool install --global dotnet-svcutil --version 2.0.3

3. 执行 dotnet restore

dotnet restore

4. 以获取国内手机号码归属地信息为例(网址:http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx),生成代理类

dotnet svcutil http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl

5. 再次执行 dotnet restore,然后修改 Program.cs

using System;
using System.ServiceModel;
using System.Threading.Tasks;
using ServiceReference;

namespace WebServiceConsole
{
    class Program
    {
        static async Task Main(string[] args)
        {
            var client = new MobileCodeWSSoapClient(MobileCodeWSSoapClient.EndpointConfiguration.MobileCodeWSSoap);

            //// 获得国内手机号码归属地数据库信息
            //var x = await client.getDatabaseInfoAsync();

            //foreach(var str in x)
            //{
            //    Console.WriteLine(str);
            //}

            // 获得国内手机号码归属地省份、地区和手机卡类型信息
            var y = await client.getMobileCodeInfoAsync("1388888", "");
            Console.WriteLine(y);

            Console.ReadLine();
        }
    }
}

6. 保存,Ctrl+F5,然后结果:

1388888:云南 昆明 云南移动合家欢卡

这是一个简单的事例。

参考:

https://www.qiufengblog.com/articles/dotnet-core-webservice.html

http://www.webxml.com.cn/zh_cn/index.aspx (http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx)

https://docs.microsoft.com/en-us/dotnet/core/additional-tools/dotnet-svcutil-guide?tabs=dotnetsvcutil2x

https://www.cnblogs.com/myzony/p/9547254.html

posted @ 2021-11-21 22:23  Bruce_Cheung  阅读(596)  评论(2编辑  收藏  举报