vb.net网站登录

时间:2013-01-24 16:20:57

标签: vb.net post login html-agility-pack

我正在尝试使用vb.net中的HTML Agility Pack解析网页。当我调用要解析的页面时,我会被重定向到登录页面。这不是问题,因为我在这个网站上有一个帐户,但我需要知道如何从我的程序中登录这个页面。到目前为止我的代码如下:

Dim logincookie As CookieContainer
Public cookies As New CookieContainer

 Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
    'Web Scrape Balance Sheet - Quarterly
    Dim wreqBalQtr As HttpWebRequest = WebRequest.Create("http://www.reuters.com/finance/stocks/incomeStatement/detail?stmtType=BAL&perType=INT&symbol=goog")
    wreqBalQtr.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5"
    wreqBalQtr.Method = "get"
    Dim proxBalQtr As IWebProxy = wreqBalQtr.Proxy
    proxBalQtr.Credentials = CredentialCache.DefaultCredentials
    Dim documentBalQtr As New HtmlAgilityPack.HtmlDocument
    Dim webBalQtr As New HtmlAgilityPack.HtmlWeb
    webBalQtr.UseCookies = True
    webBalQtr.PreRequest = New HtmlAgilityPack.HtmlWeb.PreRequestHandler(AddressOf onPreReq)
    wreqBalQtr.CookieContainer = cookies
    Dim resBalQtr As HttpWebResponse = wreqBalQtr.GetResponse()
    documentBalQtr.Load(resBalQtr.GetResponseStream, True)

    Dim str As String
    str = documentBalQtr.DocumentNode.OuterHtml
    If str.Contains("Enter your Reuters.com account info") = True Then

        'NEED TO LOG INTO THE PAGE THAT IS CURRENTLY PULLED UP

        Dim TotalCurrentAssets = documentBalQtr.DocumentNode.SelectSingleNode("//table[@class='dataTable financials']/tr[13]/td[3]")
        Dim TotalCurrentLiabilities = documentBalQtr.DocumentNode.SelectSingleNode("//table[@class='dataTable financials']//tr[29]//td[1]")
        MsgBox(TotalCurrentAssets.InnerText)
    Else
        Dim TotalCurrentAssets = documentBalQtr.DocumentNode.SelectSingleNode("//table[@class='dataTable financials']/tr[13]/td[3]")
        Dim TotalCurrentLiabilities = documentBalQtr.DocumentNode.SelectSingleNode("//table[@class='dataTable financials']//tr[29]//td[1]")
        MsgBox(TotalCurrentAssets.InnerText)
    End If

End Sub

Private Function onPreReq(ByVal req As HttpWebRequest)

    req.CookieContainer = cookies
    Return True

End Function

1 个答案:

答案 0 :(得分:0)

您可以使用基础.NET库(HTTP类)。我还会看一下WCF(Windows Communication Foundation),看它是否有更简单的方法。

如果运气不错,该网站正在使用HTTP基本身份验证,这很容易编程。我会检查HTML和HTTP标头,以查看要调用的身份验证方法。