PowerShell与万维网的深度探索
1. 访问需要身份验证的资源
在网络环境中,安全至关重要。很多时候,我们需要访问那些在访问前需要进行身份验证的文件。.NET Framework 和 System.Net.WebClient 支持万维网联盟定义的 HTTP 身份验证机制,以及 NTLM(NT LAN Manager)身份验证,也就是 Windows 集成身份验证。配置客户端使用正确的凭据并不复杂。
在设置凭据之前,我们先看看在不传递正确凭据的情况下尝试访问受保护资源会发生什么。以 http://www.pagetutor.com 的测试页面为例,受保护区域的 URL 是 http://www.pagetutor.com/keeper/mystash/secretstuff.html 。当我们使用 Internet Explorer 等普通浏览器请求该页面时,会弹出密码提示框。
使用 System.Net.WebClient 时,情况如下:
PS> $url = "http://www.pagetutor.com/keeper/mystash/secretstuff.html"
PS> .\Get-Url.ps1 $url
Exception calling "DownloadString" with "1" argument(s): "The remote s
erver returned an error: (401) Unauthorized."
At
超级会员免费看
订阅专栏 解锁全文
4216

被折叠的 条评论
为什么被折叠?



