在PowerShell中找不到[System.Web.HttpUtility]类型

41

我正在使用PowerShell为Microsoft Translator应用程序获取访问令牌,但在此过程中某些命令因错误而失败:

Unable to find type [System.Web.HttpUtility]

在收到这个错误之前,我将代码从这个 MSDN 页面复制粘贴到 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)
# ...

为使此功能正常运行,我需要添加哪些额外的代码?


1
你看过这个吗?https://dev59.com/VF4c5IYBdhLWcg3wdKKT - Aravind
1个回答

96

您需要加载 System.Web 组件。使用以下方式使用 Add-Type 命令:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接