【问题标题】:Power Shell Web Scraping SSL/TSL IssuePowershell Web Scraping SSL/TLS 问题
【发布时间】:2012-04-12 15:46:10
【问题描述】:

我想在服务器上运行网络抓取脚本。

当前脚本收集指定页面的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 页面中抓取元素。

谢谢各位!

【问题讨论】:

    标签: html powershell web-scraping


    【解决方案1】:

    这一行将忽略 SSL 证书错误:

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

    执行此操作后,将忽略有关自签名不受信任证书、名称不匹配或过期的错误。

    【讨论】:

    • 而要恢复,只需执行[System.Net.ServicePointManager]::ServerCertificateValidationCallback = $null
    • 此答案不再有效,请参阅原因和解决方法here
    【解决方案2】:

    使用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);
    

    【讨论】:

    • 感谢 Mathias,能否将其转换为 Power Shell 脚本格式?
    • 在 PoSH 中运行时只需从源代码编译即可,如下所示:poshcode.org/624
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-25
    • 1970-01-01
    • 2023-02-24
    • 2018-04-07
    • 1970-01-01
    • 2019-09-06
    • 2012-11-15
    相关资源
    最近更新 更多