DirectCast Timeout issue HttpWebResponse

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • stewart.fay@anite.com

    #1

    DirectCast Timeout issue HttpWebResponse

    Dear all,

    I am using the HttpWebRequest and HttpWebRequest to send an XML request
    to an ASPX page.

    When I use the browser and add my XML request the request is posted
    fine and an answer is retrieved.

    However, when I use the code below I get an timeout error on the line:
    oWebResponse = DirectCast(oWeb Request.GetResp onse, HttpWebResponse )

    Does anyone have any ideas why this timeouts in the code, but not the
    browser?

    Code:
    'Issue the Request
    Try
    oWebRequest =
    DirectCast(WebR equest.Create(m _sURIString), HttpWebRequest)
    oWebRequest.Met hod = "POST"
    oWebRequest.Con tentType = "text/xml"
    oWebRequest.Kee pAlive = False 'False cos were only
    sending one request
    oWebRequest.Con tentLength = sRequest.Length
    oWebRequest.Tim eout = iTimeout
    oStmWriter = New
    StreamWriter(oW ebRequest.GetRe questStream)

    oStmWriter.Writ e(sRequest)
    oStmWriter.Flus h() 'make sure its done

    LogMessage(m_oE ventLog, "Posted request - " +
    m_sXMLFile)

    Catch WebEx As Net.WebExceptio n
    Throw New AppException("G enerated Web Exception:" &
    WebEx.Message.T oString, _
    ExceptionLevel. Failure, "Sending Request to
    URI")
    Catch ex As Exception
    Throw New AppException(ex .Message,
    ExceptionLevel. Failure, "Sending Request to URI")
    End Try

    'Now read the repsonse
    Try
    'Read the response coming back from the website
    oWebResponse = DirectCast(oWeb Request.GetResp onse,
    HttpWebResponse )
    oStmReader = New
    StreamReader(oW ebResponse.GetR esponseStream)
    'Set the Response to a nice string
    sResponse = oStmReader.Read ToEnd
    LogMessage(m_oE ventLog, "Response recieved - " +
    m_sXMLFile)

    'Put the XML String into an XML document so that we can
    read it nicely
    oXMLResponse.Lo adXml(sResponse )

    Finally
    oStmWriter.Clos e()
    If Not IsNothing(oStmR eader) Then
    oStmReader.Clos e()
    End If

    If Not IsNothing(oWebR esponse) Then
    oWebResponse.Cl ose()
    oWebResponse = Nothing
    End If

    oStmWriter = Nothing
    oStmReader = Nothing
    oWebRequest = Nothing
    oXML = Nothing
    End Try

  • dotNetDave

    #2
    RE: DirectCast Timeout issue HttpWebResponse

    I have a silly response, why are you doing it this way instead of just adding
    the web service as a Web Reference?

    =============== =============== ========
    David McCarter

    VSDN Tips & Tricks .NET Coding Standards available at:
    www.cafepress.com/vsdntips.20412485


    "stewart.fay@an ite.com" wrote:
    Dear all,
    >
    I am using the HttpWebRequest and HttpWebRequest to send an XML request
    to an ASPX page.
    >
    When I use the browser and add my XML request the request is posted
    fine and an answer is retrieved.
    >
    However, when I use the code below I get an timeout error on the line:
    oWebResponse = DirectCast(oWeb Request.GetResp onse, HttpWebResponse )
    >
    Does anyone have any ideas why this timeouts in the code, but not the
    browser?
    >
    Code:
    'Issue the Request
    Try
    oWebRequest =
    DirectCast(WebR equest.Create(m _sURIString), HttpWebRequest)
    oWebRequest.Met hod = "POST"
    oWebRequest.Con tentType = "text/xml"
    oWebRequest.Kee pAlive = False 'False cos were only
    sending one request
    oWebRequest.Con tentLength = sRequest.Length
    oWebRequest.Tim eout = iTimeout
    oStmWriter = New
    StreamWriter(oW ebRequest.GetRe questStream)
    >
    oStmWriter.Writ e(sRequest)
    oStmWriter.Flus h() 'make sure its done
    >
    LogMessage(m_oE ventLog, "Posted request - " +
    m_sXMLFile)
    >
    Catch WebEx As Net.WebExceptio n
    Throw New AppException("G enerated Web Exception:" &
    WebEx.Message.T oString, _
    ExceptionLevel. Failure, "Sending Request to
    URI")
    Catch ex As Exception
    Throw New AppException(ex .Message,
    ExceptionLevel. Failure, "Sending Request to URI")
    End Try
    >
    'Now read the repsonse
    Try
    'Read the response coming back from the website
    oWebResponse = DirectCast(oWeb Request.GetResp onse,
    HttpWebResponse )
    oStmReader = New
    StreamReader(oW ebResponse.GetR esponseStream)
    'Set the Response to a nice string
    sResponse = oStmReader.Read ToEnd
    LogMessage(m_oE ventLog, "Response recieved - " +
    m_sXMLFile)
    >
    'Put the XML String into an XML document so that we can
    read it nicely
    oXMLResponse.Lo adXml(sResponse )
    >
    Finally
    oStmWriter.Clos e()
    If Not IsNothing(oStmR eader) Then
    oStmReader.Clos e()
    End If
    >
    If Not IsNothing(oWebR esponse) Then
    oWebResponse.Cl ose()
    oWebResponse = Nothing
    End If
    >
    oStmWriter = Nothing
    oStmReader = Nothing
    oWebRequest = Nothing
    oXML = Nothing
    End Try
    >
    >

    Comment

    Working...