微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

.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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐