Quantcast
Channel: Visual Studio General Questions forum
Viewing all articles
Browse latest Browse all 21115

HTTPWebRequest Periodically freezing app.

$
0
0

I have code that I have been using for roughly 2+ years that periodically will freeze my application.  This code runs about every 20 seconds of the day.  It might run perfectly fine for a month with no problems making me think that I have figured out the issue with the code, at other times it causes the program to freeze about ever 2-3 days.  This is very frustrating I would appreciate anyones help in figuring out what is going on.  When the program freezes it's either at the "oRequest.GetRequestStream is Nothing" line or the "oRequest.GetResponse is Nothing" line.

Public Function FiveMin(ByVal cert As String, ByVal pass As String, ByVal location As String, ByVal type As String, ByVal xsdfile As String, ByVal table As String, ByVal field As String)

        Dim BUFFERLENGTH As Integer = 1024
        Dim buffer(BUFFERLENGTH) As Byte
        Dim size As Integer = 0
        Dim bytesRead As Integer = 0
        Dim sData, xmlstring As String
        Dim store As X509Store
        Dim certificates As X509Certificate2Collection
        Dim certificate As X509Certificate
        Dim oRequest As HttpWebRequest
        Dim dataStream As Stream
        Dim MemoryStream As MemoryStream
        Dim StreamWriter As StreamWriter
        Dim oResponse As WebResponse
        Dim StreamReader As StreamReader
        Dim bArray As Byte()

        'xml query
        sData = "<?xml version=""1.0"" encoding=""utf-8""?>" _& "<soap:Envelope xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"">" _& "<soap:Body>" _& "<QueryRequest xmlns=""http://markets.abc.com/art/xml"">" _& "<QueryRealTimeLMP type=""" + type + """>" _& "<LocationName>" + location + "</LocationName>" _& "</QueryRealTimeLMP>" _& "</QueryRequest>" _& "</soap:Body>" _& "</soap:Envelope>"

        'Get Certificate from the Certificate Store on the local Machine
        store = New X509Store(StoreName.My, StoreLocation.LocalMachine)
        store.Open(OpenFlags.ReadOnly)
        certificates = store.Certificates.Find(X509FindType.FindBySubjectName, cert, True)
        certificate = certificates(0)

        oRequest = DirectCast(WebRequest.Create("https://markets.abc.com/art/xml/query"), HttpWebRequest)
        oRequest.ClientCertificates.Add(certificate)
        oRequest.Method = "POST"
        'convert data to a byte array
        bArray = Encoding.ASCII.GetBytes(sData)
        'set content type
        oRequest.ContentType = "text/xml"
        oRequest.Headers.Add("SOAPAction", "/art/xml/query")
        ' Set the ContentLength property of the WebRequest.

        oRequest.Timeout = 10000
        oRequest.KeepAlive = False
              ' Get the request stream.

        MemoryStream = New MemoryStream
        StreamWriter = New StreamWriter(MemoryStream)
        StreamReader = New StreamReader(MemoryStream)

        Try
            If oRequest.GetRequestStream Is Nothing Then
                oRequest.GetRequestStream.Close()
                oRequest.GetRequestStream.Dispose()
                Return False
            Else
                dataStream = oRequest.GetRequestStream
            End If

            ' Write the data to the request stream.
            dataStream.Write(bArray, 0, bArray.Length)
            ' Close the Stream object.
            dataStream.Close()
            dataStream.Dispose()
            ' Get the response.
            If oRequest.GetResponse Is Nothing Then
                oRequest.GetResponse.Close()
                Return False
            Else
                oResponse = oRequest.GetResponse()
            End If

            dataStream = oResponse.GetResponseStream()
      
            While size <> -1
                size = dataStream.Read(buffer, 0, BUFFERLENGTH)
                If size = 0 Then Exit While
                bytesRead += size
                If size < BUFFERLENGTH Then
                    Dim buff(size) As Byte
                    Array.Copy(buffer, buff, size)
                    MemoryStream.Write(buff, 0, size)
                    buff = Nothing
                Else
                    MemoryStream.Write(buffer, 0, size)
                End If
                Array.Clear(buffer, 0, size)
            End While
            MemoryStream.Position = 0

            xmlstring = StreamReader.ReadToEnd
            If validatexml(xsdfile, xmlstring) = True Then
                FiveMinImport(xmlstring, table, field)
            Else
                Return False
            End If
            oResponse.Close()
            oRequest.Abort()
            dataStream.Close()

        Catch VEex As ProtocolViolationException
            oRequest.Abort()
            Return False
        Catch ex As NullReferenceException
            oRequest.Abort()
            Return False
        Catch webex As WebException
            oRequest.Abort()
            Return False
        Catch ioex As IOException
            oRequest.Abort()
            Return False
        Finally

            oResponse.Close()
            MemoryStream.Close()
            MemoryStream.Dispose()
            StreamWriter.Flush()
            StreamWriter.Close()
            StreamWriter.Dispose()
            StreamReader.Close()
            StreamReader.Dispose()
            oRequest.Abort()
            dataStream.Close()
        End Try
        oRequest.Abort()
        Return DLCheck
    End Function

Any other mistakes in my code I would appreciate it if you could point it out.


Viewing all articles
Browse latest Browse all 21115

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>