如何一次发送多个xml / HTTP请求并获取结果字符串? Vb.net

时间:2014-08-20 15:16:41

标签: vb.net http getresponse

所以我有一台IP摄像机,它有我想要的信息(日期,固件版本等),每一条信息都是通过从http URL转到Http.GetResponse()并获取XML响应字符串来获取的。例如,“http://”& ip& “/ System / Audio / Channels”为您提供有关其音频通道的字符串。

我需要大量信息,但设备没有列出我需要的所有项目的URL,因此我会为每个设备重复此过程,其中包含x个项目特定的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

显然,在针对特定网址的第10次循环之后,您开始变得缓慢且重复。

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

1 个答案:

答案 0 :(得分: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