Home > database >  Translate C# to VB.NET 'ServerCertificateValidationCallback' is not an event of 'Http
Translate C# to VB.NET 'ServerCertificateValidationCallback' is not an event of 'Http

Time:03-28

I'm trying to convert this code to VB.NET:

public static void Main(string[] args)
    {
        var myProxy = new WebProxy("http://proxy.zyte.com:8011", true);
        myProxy.Credentials = new NetworkCredential("<CRAWLERA_APIKEY>", "");

        var request = (HttpWebRequest)WebRequest.Create("https://httpbin.scrapinghub.com/headers");
        request.Proxy = myProxy;
        request.PreAuthenticate = true;
        request.AllowAutoRedirect = false;
        request.ServerCertificateValidationCallback  = (sender, certificate, chain, sslPolicyErrors) => true;

        var response = request.GetResponse();
        Console.WriteLine("Response Status: "   ((HttpWebResponse)response).StatusDescription);
        Console.WriteLine("\nResponse Headers:\n"   ((HttpWebResponse)response).Headers);
        var dataStream = response.GetResponseStream();
        var reader = new StreamReader(dataStream);
        string responseFromServer = reader.ReadToEnd();
        Console.WriteLine("Response Body:\n"   responseFromServer);
        reader.Close();
        response.Close();
    }

What I have so far is:

Public Shared Sub Main(ByVal args As String())
        Dim myProxy = New WebProxy("http://proxy.zyte.com:8011", True)
        myProxy.Credentials = New NetworkCredential("<CRAWLERA_APIKEY>", "")
        Dim request = CType(WebRequest.Create("https://httpbin.scrapinghub.com/headers"), HttpWebRequest)
        request.Proxy = myProxy
        request.PreAuthenticate = True
        request.AllowAutoRedirect = False
        AddHandler request.ServerCertificateValidationCallback, Function(sender, certificate, chain, sslPolicyErrors) True
        Dim response = request.GetResponse()
        Console.WriteLine("Response Status: " & (CType(response, HttpWebResponse)).StatusDescription)
        Console.WriteLine(vbLf & "Response Headers:" & vbLf & (CType(response, HttpWebResponse)).Headers)
        Dim dataStream = response.GetResponseStream()
        Dim reader = New StreamReader(dataStream)
        Dim responseFromServer As String = reader.ReadToEnd()
        Console.WriteLine("Response Body:" & vbLf & responseFromServer)
        reader.Close()
        response.Close()
    End Sub

This fails on line: AddHandler request.ServerCertificateValidationCallback, Function(sender, certificate, chain, sslPolicyErrors) True Where I get error

'ServerCertificateValidationCallback' is not an event of 'HttpWebRequest'.

I'm not sure how to translate it.

CodePudding user response:

As the error message says, that member is not an event, so you cannot handle it. It's a property, so you have to assign to it like any other property. It's type is RemoteCertificateValidationCallback, which is a delegate, i.e. an object that refers to a method. You need to create a delegate of that type and assign it to the property:

request.ServerCertificateValidationCallback = Function(sender, certificate, chain, sslPolicyErrors) True
  • Related