代码改变世界

powershell中使用New-WebServiceProxy调用webapi(webservice使用可,wcf使用不可)

2016-02-19 14:10  whitecf  阅读(849)  评论(0编辑  收藏  举报

无认证的webapi调用

$uri = "http://www.holidaywebservice.com/HolidayService_v2/HolidayService2.asmx"
$proxy = New-WebServiceProxy -Uri $url
$proxy.GetCountriesAvailable() #webapi的方法

带WCF的客户端认证的webapi调用

$uri = "http://www.holidaywebservice.com/HolidayService_v2/HolidayService2.asmx"
$userName="user01"
$password="123456"
$credentials = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $userName, (ConvertTo-SecureString $password -AsPlainText -Force)
$proxy = New-WebServiceProxy -Uri $url -Credentials $credentials
$proxy.GetCountriesAvailable() #webapi的方法

说明:(ConvertTo-SecureString $password -AsPlainText -Force) 密码必须转成密文(SecureString)才能用