在VB.Net应用程序的第40次迭代中关闭了基础连接

时间:2018-06-26 08:59:05

标签: vb.net httpwebrequest

此问题在StackExchange上多次出现,但我无法解决。大多数答案说,这是由于SSL或TLS问题以及将协议设置为TLS10或使用KeepAlive引起的。

就我而言,我正在调用自己的PHP终结点,而不使用SSL。该服务器托管在GoDaddy上。

我正在从服务器检索记录。由于返回的数据量很大,因此将调用置于循环中。循环运行并在抛出此错误之前获取40-50次迭代的数据。这不是超时问题,因为错误是在毫秒内引发的。

我怀疑流或连接未关闭,并且VB.Net程序资源不足或服务器打开的连接过多。

下面的代码略有删节,以删除敏感信息:

        While True

        ' Create the request
        uri = New Uri(My.Settings.ServerURL & My.Settings.GetEmployeeRecords)
        request = DirectCast(WebRequest.Create(uri), HttpWebRequest)
        ' Add user credentials
        creds = New CredentialCache
        creds.Add(uri, "Basic", New NetworkCredential(My.Settings.UserId, My.Settings.Password))
        With request
            .Method = "POST"
            .ContentType = "application/x-www-form-urlencoded"
            .AutomaticDecompression = DecompressionMethods.GZip + DecompressionMethods.Deflate
            .Credentials = creds
            .KeepAlive = False
            .ProtocolVersion = HttpVersion.Version10
            .ConnectionGroupName = Guid.NewGuid().ToString()
            .UserAgent = "VB.NET App"
            .AllowAutoRedirect = False
        End With
        ' Add parameters
        strPostData = String.Format("offset={0}&limit={1}", iOffset, iLimit)
        request.ContentLength = strPostData.Length

        Try
            Using sw As New StreamWriter(request.GetRequestStream)
                sw.Write(strPostData)
                sw.Close()
            End Using
        Catch ex As Exception
            e.Result = "Error Setting Request Data"
            Exit Sub
        End Try

        ' Send the request to the server
        Try
            response = DirectCast(request.GetResponse, HttpWebResponse)
        Catch ex As WebException
            e.Result = "Error Sending Request" **<-- This is where it is thrown**
            Exit Sub
        End Try

        ' Open the response
        Try
            reader = New StreamReader(response.GetResponseStream)
        Catch ex As Exception
            e.Result = "Error Reading Request"
            Exit Sub
        End Try

        ' Read the full response
        rawresp = reader.ReadToEnd()
        reader.Close()
        response.Close()

        ' We should never get a blank response
        If rawresp = "" Or rawresp = "[]" Then
            e.Result = "Blank Response"
            Exit Sub
        End If

        ' The response should be in JSON. Parse it
        Try
            jResults = Linq.JObject.Parse(rawresp)
        Catch ex As Exception
            e.Result = "Error parsing response"
            Exit Sub
        End Try

        ' Get the complete response into jResults
        ' The jResults would contain {statuscode, statusDescription, data[]} where the data element would be an array of employees
        ' Check for error returned in response JSON
        If jResults("statusCode").ToString = "404" Then
            Exit While
        End If
        If jResults("statusCode").ToString <> "0" Then
            e.Result = "Error received from server"
            Exit Sub
        End If

        ' Get the array for the employee records
        Try
            jEmployees = Linq.JArray.Parse(jResults("data").ToString)
        Catch ex As Exception
            e.Result = "Response Does Not Contain Employee Array"
            Exit Sub
        End Try
        ' Everything is fine. Add the recordds to the local database
        SaveEmployeesToLocalDB(jEmployees)
        iCount = jEmployees.Count
        iOffset += iCount
        iTotalRecords += iCount
        If iCount = 0 Then
            Exit While
        End If
        If iTotalRecords Mod (20 * iLimit) = 0 Then
            Application.DoEvents()
            Threading.Thread.Sleep(3000)
        End If
    End While

0 个答案:

没有答案