View Single Post
  #9 (permalink)  
Old 08-02-2007, 02:57 AM
prasannavigneshr prasannavigneshr is offline
D-Web Incredible
 
Join Date: Feb 2007
Posts: 1,321
prasannavigneshr is on a distinguished road
Send a message via MSN to prasannavigneshr
Default Re: HTTP Web Response Exception - I cant able to trap.

Hi john, thanks for your concern,

I have stripped my code to the bare minimum to re-produce the problem, the code is below. Create a new VB. NET project, insert at textbox (TextBox1), label (Label1), and 2 menu items (MenuItem1 and MenuItem2) on the form , then replace the code with the code below. Run program, enter the URL of the file to download in the text box and press MenuItem1 to download.

Once a GPRS connection is estabished and downloading has began, press and hold End Call to close GPRS. The error message I posted earlier will appear.

Code:
Public Class Form1

  Private WithEvents m_dm As New DownloadManager

 

  Private Sub MenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem2.Click

    m_dm.Abort()

    Me.Close()

  End Sub

 

  Private Sub MenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem1.Click

    Me.MenuItem1.Enabled = False

    Me.Label1.Text = "Downloading..."

    Me.m_dm.DownloadProduct(Me.TextBox1.Text)

  End Sub

 

  Private m_receivedBytes As Integer

  Private Sub m_downloadService_ProductChuckReceived(ByVal receivedBytes As Integer) Handles m_dm.ProductChuckReceived

    m_receivedBytes = receivedBytes

    Me.Invoke(New EventHandler(AddressOf ProductChuckReceived))

  End Sub

 

  Private Sub ProductChuckReceived(ByVal sender As Object, ByVal e As EventArgs)

    Me.Label1.Text = Convert.ToInt32((m_receivedBytes / m_dm.FileSize) * 100) & "%"

  End Sub

 

  Private Sub m_downloadService_ProductDownloadedCompleted() Handles m_dm.ProductDownloadedCompleted

    Me.Invoke(New EventHandler(AddressOf ProductDownloadedCompleted))

  End Sub

 

  Private Sub ProductDownloadedCompleted(ByVal sender As Object, ByVal e As EventArgs)

    Me.Label1.Text = "Download complete"

  End Sub

End Class

 

Public NotInheritable Class DownloadManager

  Private Const DATA_BLOCK_SIZE As Integer = 65536

  Public Event ProductDownloadedCompleted()

  Public Event ProductChuckReceived(ByVal receivedBytes As Integer)

  Private m_urlRequest As Net.HttpWebRequest = Nothing

  Private m_urlResponse As Net.HttpWebResponse

  Private m_data() As Byte

  Private m_filename As String = ""

  Private m_folder As String = "\"

  Private m_fileSize As Integer

  Private m_fs As IO.FileStream

  Private m_abort As Boolean = False

  Private m_downloading As Boolean = False

  Private m_errMsg As String = ""

 

  Public Sub DownloadProduct(ByVal url As String)

    m_abort = False

    m_filename = "\" & url.Substring(url.LastIndexOf("/"c) + 1)

    m_downloading = True

    m_urlRequest = CType(Net.HttpWebRequest.Create(url), Net.HttpWebRequest)

    m_urlRequest.Timeout = 20000

    m_urlRequest.Method = "GET"

    m_urlRequest.KeepAlive = False

    m_urlRequest.BeginGetResponse(New AsyncCallback(AddressOf ResponseReceived), Nothing)

  End Sub

 

  Private Sub ResponseReceived(ByVal ar As IAsyncResult)

    If m_abort = False Then

    Try

      m_urlResponse = CType(m_urlRequest.EndGetResponse(ar), Net.HttpWebResponse)

      m_data = New Byte(DATA_BLOCK_SIZE) {}

      m_fileSize = Convert.ToInt32(m_urlResponse.ContentLength)

      If IO.File.Exists(m_filename) Then

        IO.File.Delete(m_filename)

      End If

      m_fs = New IO.FileStream(m_filename, IO.FileMode.Create)

      Catch ex As Net.WebException

        m_errMsg = ex.Message

      Catch ex As IO.IOException

        m_errMsg = ex.Message

      End Try

    End If

 

    If m_abort OrElse m_errMsg <> "" Then

    Else

      RaiseEvent ProductChuckReceived(0)

      m_urlResponse.GetResponseStream().BeginRead(m_data, 0, DATA_BLOCK_SIZE, New AsyncCallback(AddressOf OnDataRead), Me)

    End If

  End Sub

 

  Sub OnDataRead(ByVal res As IAsyncResult)

    If m_abort = False Then

      Dim bytesWrite As Integer = m_urlResponse.GetResponseStream().EndRead(res)

      m_fs.Write(m_data, 0, bytesWrite)

      If bytesWrite > 0 Then

        If m_abort = False Then

          RaiseEvent ProductChuckReceived(Convert.ToInt32(m_fs.Position))

        Try

          m_urlResponse.GetResponseStream().BeginRead(m_data, 0, DATA_BLOCK_SIZE, New AsyncCallback(AddressOf OnDataRead), Me)

          Catch ex As Exception

        End Try

      End If

    Else

      If m_fs.Length <> Me.m_fileSize Then

        m_abort = True

        Me.m_errMsg = "Error downloading file."

      End If

      m_urlResponse.Close()

      m_urlRequest.Abort()

      m_urlResponse.GetResponseStream.Close()

      If m_abort = False Then

        m_fs.Close()

        m_fs = Nothing

        RaiseEvent ProductDownloadedCompleted()

      End If

    End If

  End If

  If m_abort Then

    m_fs.Close()

    m_fs = Nothing

    Try

      If IO.File.Exists(m_filename) Then

        IO.File.Delete(m_filename)

      End If

    Catch ex As Exception

    End Try

  End If

End Sub

 

Public Sub Abort()

  m_abort = True

  If m_downloading Then

    m_urlRequest.Abort()

    m_urlRequest = Nothing

    m_urlResponse.Close()

    m_urlResponse = Nothing

  End If

End Sub

 

Public ReadOnly Property FileSize() As Integer

  Get

    Return m_fileSize

  End Get

End Property

 

  Public Function Aborted() As Boolean

    Return m_abort

  End Function

End Class
Thanks buddy,
__________________
Prasanna Vignesh
MCPD | Web Developer
Reply With Quote