2012-03-29 49 views
5

我想在服务器上运行Web抓取脚本。Power Shell Web Scraping SSL/TSL问题

当前脚本收集指定页面上的html。

$url = "http://websms" 
[net.httpWebRequest] $request = [net.webRequest]::create($url) 
[net.httpWebResponse] $response = $request.getResponse() 
$responseStream = $response.getResponseStream() 
$sr = new-object IO.StreamReader($responseStream) 
$result = $sr.ReadToEnd() 

$result 

这在一个典型的网页上正常工作。不过,我想在服务器管理页面上运行它,当然这需要登录。

我想在我尝试登录之前,我会尝试和刮去服务器的登录页面。运行上面的脚本,我得到以下结果。

Exception calling "GetResponse" with "0" argument(s): "The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel." 
At C:\temp\web3.ps1:3 char:56 
+ [net.httpWebResponse] $response = $request.getResponse <<<<() 
    + CategoryInfo   : NotSpecified: (:) [], MethodInvocationException 
    + FullyQualifiedErrorId : DotNetMethodException 

如何来解决这个问题,或者是如果你能指出我不同的方向,所以我可以刮去的服务器的管理员HTML页面元素的任何想法。

谢谢!

回答

23

这一个班轮将忽略SSL证书错误:关于自签名不受信任的证书,不匹配的姓名或到期

[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true} 

错误将在此之后被忽略被执行。

+0

非常感谢! – 2012-03-29 01:54:39

+7

并恢复,只需执行'[System.Net.ServicePointManager] :: ServerCertificateValidationCallback = $ null' – mousio 2012-12-20 14:17:43

+0

非常感谢! – hupseb 2014-06-24 10:07:41

0

使用this brilliant answer

public bool AcceptAllCertifications(object sender, System.Security.Cryptography.X509Certificates.X509Certificate certification, System.Security.Cryptography.X509Certificates.X509Chain chain, System.Net.Security.SslPolicyErrors sslPolicyErrors) 
{ 
    return true; 
} 
ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(AcceptAllCertifications); 
+0

感谢Mathias,可以将它转换为Power Shell Script格式吗? – 2012-03-29 01:33:37

+0

只需在PoSH中运行时从源代码编译它,如下所示:http://poshcode.org/624 – 2012-03-29 01:39:30

+0

酷感谢队友! – 2012-03-29 01:54:52