Home All Groups Group Topic Archive Search About

DirectCast Timeout issue HttpWebResponse

Author
2 Aug 2006 1:08 PM
stewart.fay
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(oWebRequest.GetResponse, HttpWebResponse)

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

Code:
'Issue the Request
            Try
                oWebRequest =
DirectCast(WebRequest.Create(m_sURIString), HttpWebRequest)
                oWebRequest.Method = "POST"
                oWebRequest.ContentType = "text/xml"
                oWebRequest.KeepAlive = False 'False cos were only
sending one request
                oWebRequest.ContentLength = sRequest.Length
                oWebRequest.Timeout = iTimeout
                oStmWriter = New
StreamWriter(oWebRequest.GetRequestStream)

                oStmWriter.Write(sRequest)
                oStmWriter.Flush() 'make sure its done

                LogMessage(m_oEventLog, "Posted request - " +
m_sXMLFile)

            Catch WebEx As Net.WebException
                Throw New AppException("Generated Web Exception:" &
WebEx.Message.ToString, _
                        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(oWebRequest.GetResponse,
HttpWebResponse)
                oStmReader = New
StreamReader(oWebResponse.GetResponseStream)
                'Set the Response to a nice string
                sResponse = oStmReader.ReadToEnd
                LogMessage(m_oEventLog, "Response recieved - " +
m_sXMLFile)

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

            Finally
                oStmWriter.Close()
                If Not IsNothing(oStmReader) Then
                    oStmReader.Close()
                End If

                If Not IsNothing(oWebResponse) Then
                    oWebResponse.Close()
                    oWebResponse = Nothing
                End If

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

Author
2 Aug 2006 6:44 PM
dotNetDave
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
www.vsdntips.com
VSDN Tips & Tricks .NET Coding Standards available at:
www.cafepress.com/vsdntips.20412485


Show quoteHide quote
"stewart.***@anite.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(oWebRequest.GetResponse, HttpWebResponse)
>
> Does anyone have any ideas why this timeouts in the code, but not the
> browser?
>
> Code:
> 'Issue the Request
>             Try
>                 oWebRequest =
> DirectCast(WebRequest.Create(m_sURIString), HttpWebRequest)
>                 oWebRequest.Method = "POST"
>                 oWebRequest.ContentType = "text/xml"
>                 oWebRequest.KeepAlive = False 'False cos were only
> sending one request
>                 oWebRequest.ContentLength = sRequest.Length
>                 oWebRequest.Timeout = iTimeout
>                 oStmWriter = New
> StreamWriter(oWebRequest.GetRequestStream)
>
>                 oStmWriter.Write(sRequest)
>                 oStmWriter.Flush() 'make sure its done
>
>                 LogMessage(m_oEventLog, "Posted request - " +
> m_sXMLFile)
>
>             Catch WebEx As Net.WebException
>                 Throw New AppException("Generated Web Exception:" &
> WebEx.Message.ToString, _
>                         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(oWebRequest.GetResponse,
> HttpWebResponse)
>                 oStmReader = New
> StreamReader(oWebResponse.GetResponseStream)
>                 'Set the Response to a nice string
>                 sResponse = oStmReader.ReadToEnd
>                 LogMessage(m_oEventLog, "Response recieved - " +
> m_sXMLFile)
>
>                 'Put the XML String into an XML document so that we can
> read it nicely
>                 oXMLResponse.LoadXml(sResponse)
>
>             Finally
>                 oStmWriter.Close()
>                 If Not IsNothing(oStmReader) Then
>                     oStmReader.Close()
>                 End If
>
>                 If Not IsNothing(oWebResponse) Then
>                     oWebResponse.Close()
>                     oWebResponse = Nothing
>                 End If
>
>                 oStmWriter = Nothing
>                 oStmReader = Nothing
>                 oWebRequest = Nothing
>                 oXML = Nothing
>             End Try
>
>