0

所以我有一个包含我想要的信息(日期、固件版本等)的 IP 摄像机,并且通过从 http URL 转到 Http.GetResponse() 并获取 XML 响应字符串来获取每条信息。例如,"http://" & ip & "/System/Audio/Channels" 给你一个关于它的音频通道的字符串。

我需要很多信息,但该设备没有列出我需要的所有项目的 URL,因此我对每个设备重复此过程,其中包含 x 个项目特定 URL。以下是仅使用一个 URL 的代码片段:

  Dim RespStr As String = "http://" & ip & "/System/Audio/Channels"

   'Open Request...
    Dim HttpReq As Net.HttpWebRequest = Nothing
    Try

        'create request object
        HttpReq = CType(Net.WebRequest.Create(ReqStr), Net.HttpWebRequest)

        'set the username and password
        HttpReq.Credentials = New Net.NetworkCredential(camera.Username, camera.Password)

        'set method
        HttpReq.Method = "GET"

        'disable expect-100-continue
        HttpReq.ServicePoint.Expect100Continue = False

        'timeout
        HttpReq.Timeout = 1000 * 2

    Catch ex As Exception

    End Try

    'Open Response...
    Dim HttpResp As Net.HttpWebResponse = Nothing
    Dim HttpStatus As Net.HttpStatusCode = Nothing
           Try

            'get the response
            HttpResp = CType(HttpReq.GetResponse(), Net.HttpWebResponse)

            'verify the response
            HttpStatus = HttpResp.StatusCode
            If HttpStatus <> Net.HttpStatusCode.OK Then

               'error
            End If
      'do stuff to process xml string



        Catch ex As Exception

         End Try

显然,在特定 URL 循环第 10 次之后,您开始变得缓慢和重复。

有没有办法以更快的方式告诉 vb.net 转到 url1、url2、url3(都类似于我上面提到的示例)并在一次网络尝试中连接所有字符串响应?甚至可能一次,因为它是相同的 IP 地址?然后我可以在我的一端而不是通过网络来处理它。

4

1 回答 1

0

通过利用 .NET Framework 的并行库,您可以通过并行执行多个类似任务来加快您的进程。

文档: http: //msdn.microsoft.com/en-us/library/dd460705 (v=vs.110).aspx

不需要执行任何特殊操作,但有一些注意事项:

这是一个简短的实现:

Public Class Program

    Shared Sub Main()

        Dim urls As New ConcurrentQueue(Of String)
        urls.Enqueue("www.google.com")
        urls.Enqueue("www.yahoo.com")
        Dim myMethod As Action = Sub() 

            Dim localRequest As String
            Dim localResponse As String
            While urls.TryDequeue(localRequest)
                System.Threading.Thread.Sleep(100) 'Rate limiting, adjust to suit your needs
                localResponse = WebWorker.MakeRequest(localRequest)
                Console.WriteLine(localResponse.ToString())
                'Do something with localResponse
            End While

        End Sub
        Parallel.Invoke(New ParallelOptions() With {.MaxDegreeOfParallelism = 10 }, myMethod, myMethod, myMethod)
    End Sub

    'Do something with the responses
End Class

Public NotInheritable Class WebWorker
    Private Sub New()
    End Sub

    Public Shared Function MakeRequest(request As String) As String
        Dim response As New String()
        Dim status As Boolean
        Dim req As HttpWebRequest = Nothing
        Dim resp As HttpWebResponse = Nothing
        Dim maxIterations As Integer = 2
        Dim currentAttempt As Integer = 0

        While currentAttempt < maxIterations AndAlso status = False
            Try
                req = DirectCast(HttpWebRequest.Create(New Uri(request)), HttpWebRequest)

                Using resp = DirectCast(req.GetResponse(), HttpWebResponse)
                    If resp.StatusCode = HttpStatusCode.OK Then
                        status = True
                    Else
                        currentAttempt += 1

                    End If
                    ' end using
                End Using
            Catch ex As Exception
                currentAttempt += 1
            End Try
            ' end try/catch
        End While
        ' end while
        Return response
    End Function

End Class
于 2014-08-20T15:38:13.407 回答