Reputation: 980
Here is my code, it took me forever to write being I'm still a nube:
Imports System.Net
Imports System.Text
Imports System.IO
Public Class Form1
Dim logincookie As CookieContainer
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim postdata As String = "action=do_login&url=https%3A%2F%2Fforum.suprbay.org% 2F&quick_login=1&quick_username=USERNAME&quick_password=PASSWORD&submit=Login&quick_remember=yes"
Dim tempcookies As New CookieContainer
Dim encoding As New UTF8Encoding
Dim bytedata As Byte() = encoding.GetBytes(postdata)
Dim postreq As HttpWebRequest = DirectCast(WebRequest.Create("https://forum.suprbay.org/member.php"), HttpWebRequest)
postreq.Method = "POST"
postreq.KeepAlive = True
postreq.CookieContainer = tempcookies
postreq.ContentType = "application/x-www-forum-urlencoded"
postreq.Referer = "https://forum.suprbay.org/member.php"
postreq.UserAgent = "Mozilla/5.0 (Windows NT 6.2; rv:9.0.1) Gecko/20100101 Firefox/9.0.1"
postreq.ContentLength = bytedata.Length
Dim postreqstream As Stream = postreq.GetRequestStream()
postreqstream.Write(bytedata, 0, bytedata.Length)
postreqstream.Close()
Dim postresponse As HttpWebResponse
postresponse = DirectCast(postreq.GetResponse(), HttpWebResponse)
tempcookies.Add(postresponse.Cookies)
logincookie = tempcookies
Dim postreqreader As New StreamReader(postresponse.GetResponseStream)
Dim thepage As String = postreqreader.ReadToEnd
RichTextBox1.Text = thepage
End Sub
End Class
When I run it and click the button, I get the following error:
"The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel."
It is the official forum of PirateBay.Se which is a torrent website, and if you go to it in a regular browser you get the warning about the trust certificate, so that is probably why I'm getting the error, right? How can I ignore the trust certificates and stuff so my application can work?
Upvotes: 2
Views: 13504
Reputation: 6683
This line should ignore the trust errors on the connection, do this before you try to connect:
ServicePointManager.ServerCertificateValidationCallback = AddressOf ValidateRemoteCertificate
You will need to have this defined in your class as well:
Public Shared Function ValidateRemoteCertificate(ByVal sender As Object, ByVal certificate As X509Certificate, ByVal chain As X509Chain, ByVal sslPolicyErrors As SslPolicyErrors) As Boolean
Return True
End Function
Sorry if this is not a perfect translation to VB.Net, i had this in C# originally.
EDIT:
And yes, that is exactly why you are getting this error, the cert they have is expired.
Upvotes: 9