将 C# 翻译成 VB.NET 'ServerCertificateValidationCallback' 不是 'HttpWebRequest' 的事件
Translate C# to VB.NET 'ServerCertificateValidationCallback' is not an event of 'HttpWebRequest'
我正在尝试将此代码转换为 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();
}
我目前拥有的是:
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
在线失败:AddHandler request.ServerCertificateValidationCallback, Function(sender, certificate, chain, sslPolicyErrors) True
我在哪里出错
'ServerCertificateValidationCallback' is not an event of 'HttpWebRequest'.
我不知道怎么翻译。
如错误消息所述,该成员不是事件,因此您无法处理它。这是一个 属性,所以你必须像任何其他 属性 一样分配给它。它的类型是 RemoteCertificateValidationCallback
,这是一个委托,即引用方法的对象。您需要创建该类型的委托并将其分配给 属性:
request.ServerCertificateValidationCallback = Function(sender, certificate, chain, sslPolicyErrors) True
我正在尝试将此代码转换为 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();
}
我目前拥有的是:
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
在线失败:AddHandler request.ServerCertificateValidationCallback, Function(sender, certificate, chain, sslPolicyErrors) True
我在哪里出错
'ServerCertificateValidationCallback' is not an event of 'HttpWebRequest'.
我不知道怎么翻译。
如错误消息所述,该成员不是事件,因此您无法处理它。这是一个 属性,所以你必须像任何其他 属性 一样分配给它。它的类型是 RemoteCertificateValidationCallback
,这是一个委托,即引用方法的对象。您需要创建该类型的委托并将其分配给 属性:
request.ServerCertificateValidationCallback = Function(sender, certificate, chain, sslPolicyErrors) True