.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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM