2017-09-18 36 views
0

我正在使用刮刀,其中我需要從一個站點刮取數據。我已經厭倦了非常簡單的webclient downloadString來獲取數據,這似乎與其他網站,但有一個問題是我在下面的代碼嘗試。任何幫助將不勝感激。感謝提前。下面的代碼在VB.Net,但我很高興有一個在C#工作的解決方案以及。.Net WebClient DownloadString不能使用混合內容

Private Function GetHtml() As String 
    Dim mData As String = "" 
    Try 
     'ServicePointManager.ServerCertificateValidationCallback = New Security.RemoteCertificateValidationCallback(AddressOf ValidateServerCertificate) 
     'ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 

     With mWC 
      mData = .DownloadString("https://www.adorama.com/brands") 
     End With 
    Catch ex As Exception 
     Debug.Print(ex.Message) 

     'With CertificateValidationCallback 
     'The remote server returned an error: (403) Forbidden. 

     'Without CertificateValidationCallback 
     'The request was aborted: Could not create SSL/TLS secure channel. 
    End Try 

    Return mData 
End Function 

Private Shared Function ValidateServerCertificate(ByVal sender As Object, ByVal certificate As X509Certificate, ByVal chain As X509Chain, ByVal sslPolicyErrors As Net.Security.SslPolicyErrors) As Boolean 
    If sslPolicyErrors = Net.Security.SslPolicyErrors.None Then 
     Return True 
    End If 

    Return True 
End Function 
+0

您能否提出更多關於您的問題的信息。你想做什麼?如果其他網站的工作是什麼這個錯誤? –

+0

@ Youssed13已解決我的問題。請檢查他的答案。感謝您給我的問題留出時間。 – AmitSri

回答

1

在使用.DownloadString()方法之前,您必須爲此網站添加User-Agent。

mWC.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:55.0) Gecko/20100101 Firefox/55.0") 
+0

謝謝,你救了我的一天。 – AmitSri