Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

HTTPWebResponse Hangs with Seemingly no timeout

Status
Not open for further replies.

skibum019

IS-IT--Management
Dec 13, 2001
25
US
I have an application that is sending XML files up to a customer's site using the httpwebrequest and httpwegresponse objects. It has worked, hit and miss, but most recently I have run into a situation where assigning the httpwebrequest.getresponse to the httpwebresponse object seemingly just hangs and does nothing. An exception is not thrown and everything grinds to a hault. I have looked through a number of sites looking for info on this, but have not been able to find relevant articles. Any help would be much apreciated.
This is being written on a Windows Vista machine with Visual Studio 2005. I have tried this both behind and in front of our firewall with the same results. It might also be beneficial to have some way of tracing the request to see where it is hanging, but not sure how best to accomplish that.
Code:
    Public Function SendData2CustA(ByVal strURL As String, ByVal strFileName As String) As String
        Dim webReq As HttpWebRequest
        Dim i As Integer, mySTR As String
        Dim mystore As New X509Store(StoreLocation.CurrentUser)
        Dim certificate As X509Certificate2
        If UserCert = "" Then
            MsgBox("You must specify a SSL Certificate to use in the transmission.  " & _
                "Go to the options page and specify an appropriate certificate.", MsgBoxStyle.Exclamation, _
                "Certificate Missing")
            SendData2CustA = "ERROR,NO CERT,NO RESPONSE"
            Exit Function
        End If
        Try
            mystore.Open(OpenFlags.ReadOnly)
            i = 0 : mySTR = ""
            webReq = WebRequest.Create(strURL)
            webReq.KeepAlive = False
            webReq.Method = "POST"
            webReq.AllowAutoRedirect = True
            webReq.ContentType = "text/xml"
            webReq.Timeout = 100000
            webReq.AllowWriteStreamBuffering = True
            System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Ssl3
            Dim instance As New WebProxy
            Dim host As New Uri(strURL)
            Dim returnValue As Boolean
            returnValue = instance.IsBypassed(host)
            For Each certificate In mystore.Certificates
                If certificate.Subject.ToString = UserCert Then
                    Exit For
                End If
                i = i + 1
            Next
            certificate = mystore.Certificates(i)
            webReq.ClientCertificates.Add(certificate)
            Dim myRdr As StreamReader = New StreamReader(strFileName)
            Dim myWrtr As StreamWriter = New StreamWriter(webReq.GetRequestStream)
            myWrtr.Write(myRdr.ReadToEnd)
            Dim myRsp As HttpWebResponse
            If myRsp Is Nothing = False Then myRsp.Close()
            myRsp = webReq.GetResponse()
            Dim mySR As New StreamReader(myRsp.GetResponseStream)
            SendData2CustA = myRsp.StatusCode & "," & _
                myRsp.StatusDescription & "," & mySR.ReadToEnd
            myWrtr.Close()
            myRdr.Close()
        Catch ex As Exception
            SendData2CustA = "ERROR," & ex.Message & "," & ex.GetBaseException.ToString
        End Try
    End Function
 
OK, so it looks like I solved this issue. It seems that if I do not close my StreamReader and StreamWriter objects before attempting to create the response object, I get this infinite wait. By moving the
myWrtr.Close()
myRdr.Close()
statements above the
myRsp = webReq.GetResponse()
and below my creation of the StreamReader and StreamWriter objects, I was able to get the responses from the server.
Code:
    Public Function SendData2CustA(ByVal strURL As String, ByVal strFileName As String) As String
        Dim webReq As HttpWebRequest
        Dim i As Integer, mySTR As String
        Dim mystore As New X509Store(StoreLocation.CurrentUser)
        Dim certificate As X509Certificate2
        If UserCert = "" Then
            MsgBox("You must specify a SSL Certificate to use in the transmission.  " & _
                "Go to the options page and specify an appropriate certificate.", MsgBoxStyle.Exclamation, _
                "Certificate Missing")
            SendData2CustA = "ERROR,NO CERT,NO RESPONSE"
            Exit Function
        End If
        Try
            mystore.Open(OpenFlags.ReadOnly)
            i = 0 : mySTR = ""
            webReq = WebRequest.Create(strURL)
            webReq.KeepAlive = False
            webReq.Method = "POST"
            webReq.AllowAutoRedirect = True
            webReq.ContentType = "text/xml"
            webReq.Timeout = 100000
            webReq.AllowWriteStreamBuffering = True
            System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Ssl3
            Dim instance As New WebProxy
            Dim host As New Uri(strURL)
            Dim returnValue As Boolean
            returnValue = instance.IsBypassed(host)
            For Each certificate In mystore.Certificates
                If certificate.Subject.ToString = UserCert Then
                    Exit For
                End If
                i = i + 1
            Next
            certificate = mystore.Certificates(i)
            webReq.ClientCertificates.Add(certificate)
            Dim myRdr As StreamReader = New StreamReader(strFileName)
            Dim myWrtr As StreamWriter = New StreamWriter(webReq.GetRequestStream)
            myWrtr.Write(myRdr.ReadToEnd)
            [COLOR=red]myWrtr.Close()
            myRdr.Close()[/color]            
            Dim myRsp As HttpWebResponse
            If myRsp Is Nothing = False Then myRsp.Close()
            myRsp = webReq.GetResponse()
            Dim mySR As New StreamReader(myRsp.GetResponseStream)
            SendData2CustA = myRsp.StatusCode & "," & _
                myRsp.StatusDescription & "," & mySR.ReadToEnd
        Catch ex As Exception
            SendData2CustA = "ERROR," & ex.Message & "," & ex.GetBaseException.ToString
        End Try
    End Function
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top