Problem with HttpWebRequest efter two errors ??

E

Enrico Pavesi

After that the following code give me two error (for file not present), i
always get timeout.

Any Hints?

Using SP3



Dim req As HttpWebRequest = CType(WebRequest.Create(WebPath + downloadUrl),
HttpWebRequest)

Dim resp As HttpWebResponse

req.Method = "GET"

Try

resp = CType(req.GetResponse(), HttpWebResponse)

' Retrieve response stream

Dim respStream As Stream = resp.GetResponseStream()

' Create local file

Dim wrtr As New FileStream(localFile, FileMode.Create)

' Allocate byte buffer to hold stream contents

Dim inData(4095) As Byte

' loop through response stream reading each data block

' and writing to the local file

Dim bytesRead As Integer = respStream.Read(inData, 0, inData.Length)

While bytesRead > 0

wrtr.Write(inData, 0, bytesRead)

bytesRead = respStream.Read(inData, 0, inData.Length)

End While

respStream.Close()

wrtr.Close()

Catch ex As Exception

req.Abort()

Throw ex

Finally

If (Not resp Is Nothing) Then

resp.Close()

End If

req = Nothing

End Try
 
D

David Kline [MSFT]

Enrico,

Is your application encountering a WebException? If so, you are likely
hitting the HTTP connection limit. The .NET Compact Framework has a default
HTTP connection limit of 2.

If the exception you catch is a WebException, you will need to be sure to
close the HttpWebResponse object that is contained in the
WebException.Response property. This response will contain details
regarding the error (ex: 404 for file not found) that you can display to
your users.

Since your code example has a finally block that checks to see if the
response is valid, you can add a catch handler for WebException and let the
finally handler take care of closing the response.

For more details, please see
http://blogs.msdn.com/davidklinems/archive/2004/09/10/228169.aspx

I hope this helps!
David Kline
Microsoft .NET Compact Framework

This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Error 403 - Forbidden 4
error 403 - forbidden 1
Windows XP HTTPS File Upload 0
HTTP 'Put' error 1
Request TimeOut 2
File upload Pocket PC -> Web page, code examples 1
async http upload 5
HttpWebRequest and IIS 1

Top