如何在Windows服务应用程序中将WebBrowser转换为WebClient

时间:2016-01-05 08:55:05

标签: vb.net windows-services

伙计我需要在Windows服务应用程序中将其转换为WebClient。

Dim webBrowser1 As New WebBrowser
Dim pageready As Boolean = False
Dim timer As System.Diagnostics.Stopwatch = New Stopwatch
Private aTimer As System.Timers.Timer
Dim request As HttpWebRequest = Nothing
Dim webappID As String = String.Empty
Dim appName As String = String.Empty

 Public Sub WaitForPageLoad()
    Dim dtWebApp As Data.DataTable = BLL.fnListofWebApp()
    Dim webappURL As String = String.Empty


    If (dtWebApp.Rows.Count > 0) Then
        For Each row As DataRow In dtWebApp.Rows
            webappURL = (row.Field(Of String)("application_url"))
            webappID = (row.Field(Of Integer)("web_apps_id"))
            appName = (row.Field(Of String)("application_name"))
            serverName = (row.Field(Of String)("server_name"))
            Try

                webBrowser1.Navigate("webappURL")

                request = CType(System.Net.WebRequest.Create(webappURL), System.Net.HttpWebRequest)
                request.CookieContainer = New System.Net.CookieContainer
                request.Credentials = CredentialCache.DefaultCredentials
                CType(request, HttpWebRequest).UserAgent = ".NET Framework Example Client"

                timer.Start()

                AddHandler webBrowser1.DocumentCompleted, New WebBrowserDocumentCompletedEventHandler(AddressOf PageWaiter)

                While Not pageready

                    Application.DoEvents()

                    If Not SumState = "" Then
                        pageready = True
                        SavingError()
                        SumState = ""
                    End If


                End While
                pageready = False

            Catch ex As Exception
                SumState = ex.Message
            End Try

        Next

              Else

    End If

End Sub

Public Sub SavingError()

    timer.Stop()

    'Dim timeTaken As TimeSpan = timer.Elapsed
    'Dim totalSeconds As String = timeTaken.TotalSeconds()
    'Dim RoundedOff As String = (FormatNumber(CDbl(totalSeconds), 2))

    'Saving to tblServer_Status
    Dim server As New ServerList
    Try
        Dim RightNow As Date = Now

        server.SiteCode = strSiteCode
        server.WebAppID = webappID
        server.SummaryState = "Critical"
        server.LastCheck = DateTime.Now.ToString()
        server.NextCheck = RightNow.AddMinutes(30)
        server.Duration = 0

        BLL.fnInsertWebStat(server)

    Catch ex As Exception
        SumState = ex.Message
    End Try

    timer.Reset()


    RemoveHandler webBrowser1.DocumentCompleted, New WebBrowserDocumentCompletedEventHandler(AddressOf PageWaiter)

End Sub

Public Sub PageWaiter(ByVal sender As Object, ByVal e As WebBrowserDocumentCompletedEventArgs)
    Try
        If webBrowser1.ReadyState = WebBrowserReadyState.Complete Then
            Dim response As WebResponse = request.GetResponse()

            pageready = True

            response.Close()
            timer.Stop()


            Dim server As New ServerList
            Try
                Dim RightNow As Date = Now

                server.SiteCode = strSiteCode
                server.WebAppID = webappID
                server.SummaryState = "OK"
                server.LastCheck = DateTime.Now.ToString()
                server.NextCheck = RightNow.AddMinutes(30)
                server.Duration = RoundedOff

                BLL.fnInsertWebStat(server)

            Catch ex As Exception
                SumState = ex.Message
            End Try

            timer.Reset()

            RemoveHandler webBrowser1.DocumentCompleted, New WebBrowserDocumentCompletedEventHandler(AddressOf PageWaiter)
        End If

    Catch ex As Exception
        SumState = ex.Message
    End Try
End Sub

帮我将其配置为WebClient。因为WebBroser在Windows服务应用程序中不起作用。

帮助? 我非常需要这些代码。请帮帮我。

0 个答案:

没有答案