.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 @   Bruce_Cheung  阅读(655)  评论(2编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示