簡體   English   中英

Power Shell Web Scraping SSL / TSL問題

[英]Power Shell Web Scraping SSL/TSL Issue

我想在服務器上運行Web抓取腳本。

當前腳本收集指定頁面上的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頁面刮取元素。

多謝你們!

這一個班輪將忽略SSL證書錯誤:

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

執行此操作后,將忽略有關自簽名不受信任證書,名稱不匹配或過期的錯誤。

使用這個精彩的答案

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM