【问题标题】:convert htmlweb delegate from c# to vb.net将 htmlweb 委托从 c# 转换为 vb.net
【发布时间】:2012-10-09 23:55:33
【问题描述】:

我需要将找到的这段代码 here 转换为其 vb.net 等效项,有人可以帮我吗?

代码摘录

/// <summary>
/// Represents the method that will handle the PreRequest event.
/// </summary>
public delegate bool PreRequestHandler(HttpWebRequest request);


//And they call that delegate right before making the GetResponse call:
if (PreRequest != null)
{
    // allow our user to change the request at will
    if (!PreRequest(req))
    {
        return HttpStatusCode.ResetContent;
    }              
}

HttpWebResponse resp;

try
{
    resp = req.GetResponse() as HttpWebResponse;
}

//So all you have to do is assign a delegate to PreRequest and set your timeout within that delegate:

var web = new HtmlWeb();
web.PreRequest = delegate(HttpWebRequest webRequest)
{
     webRequest.Timeout = 4;
     return true;
};
var doc = web.Load("http://www.msn.com/");

【问题讨论】:

    标签: c# vb.net delegates html-agility-pack


    【解决方案1】:
    ''' <summary> 
    ''' Represents the method that will handle the PreRequest event. 
    ''' </summary> 
    Public Delegate Function PreRequestHandler(ByVal request As HttpWebRequest) As Boolean
    
    
    'And they call that delegate right before making the GetResponse call: 
    If PreRequest IsNot Nothing Then
        ' allow our user to change the request at will 
        If Not PreRequest(req) Then
            Return HttpStatusCode.ResetContent
        End If
    End If
    
    Dim resp As HttpWebResponse
    
    Try
        resp = TryCast(req.GetResponse(), HttpWebResponse)
    End Try
    
    'So all you have to do is assign a delegate to PreRequest and set your timeout within that delegate: 
    
    Dim web = New HtmlWeb()
    web.PreRequest = Function(webRequest As HttpWebRequest)
         webRequest.Timeout = 4
         Return [True]
    End Function
    Dim doc = web.Load("http://www.msn.com/")
    

    【讨论】:

      【解决方案2】:

      转到http://converter.telerik.com/ - 粘贴您的 C# 代码并取回 VB 代码。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多