我正在尝试使用PowerShell为Microsoft Translator应用程序获取访问 token ,但是该过程中的某些命令由于错误而失败:
Unable to find type [System.Web.HttpUtility]
首先,我输入了代码,但是如果我直接将代码从the MSDN page复制粘贴到PowerShell ISE(并替换缺少的值),则会显示相同的错误:
# ...
$ClientID = '<Your Value Here From Registered Application>'
$client_Secret = ‘<Your Registered Application client_secret>'
# If ClientId or Client_Secret has special characters, UrlEncode before sending request
$clientIDEncoded = [System.Web.HttpUtility]::UrlEncode($ClientID)
$client_SecretEncoded = [System.Web.HttpUtility]::UrlEncode($client_Secret)
# ...
我是PowerShell的新手(通常使用Linux进行开发),但是我猜想它应该可以即用即用,而不必安装其他工具。如果没有,我在哪里可以找到它们?
最佳答案
您需要加载System.Web
程序集。像这样使用Add-Type
cmdlet,
PS C:\> [System.Web.HttpUtility]::UrlEncode("www.google.com")
Unable to find type [System.Web.HttpUtility].
PS C:\> Add-Type -AssemblyName System.Web
PS C:\> [System.Web.HttpUtility]::UrlEncode("www.google.com")
www.google.com