Quantcast
Channel: Web API
Viewing all articles
Browse latest Browse all 4850

Prevent WebRequest causing a exception

$
0
0

I currently have code that sends requests for a XML file from a number of remote IP addresses.

However, If the IP address cannot be reached (which is normal), the code generates a exception.

I've got a catch web exception statement however it can be a bit temperamental and cause problems when I need to set the code up with a timer.

Is there anyway of being able to stop this exception triggering and/or handle it better?

Here's the code I currently have

Sub RetrieveStatus()
        For Each row As GridViewRow In ViewDevices.Rows
            'For each row request phone's extension and call statuses
            Dim HiddenID As HiddenField = DirectCast(row.FindControl("HiddenDeviceID"), HiddenField)
            Dim Div As HtmlGenericControl = DirectCast(row.FindControl("DeviceStatus"), HtmlGenericControl)
            Dim ID = HiddenID.Value
            Using MySqlCon = GetMySqlConnection()
                Using Command As New MySqlCommand
                    Using Adapter As New MySqlDataAdapter
                        Dim Table As New DataTable
                        Command.Connection = MySqlCon
                        Command.CommandText = "SELECT * FROM device WHERE Device_ID ='" & ID & "';"
                        Adapter.SelectCommand = Command
                        Adapter.Fill(Table)
                        Try
                            Dim URL As String = "http://" & Table.Rows.Item(0).Item("IP_Address") & "/admin/spacfg.xml"
                            Dim r As WebRequest = System.Net.HttpWebRequest.Create(URL)
                            With r
                                .Credentials = New NetworkCredential("admin", "password")
                                .PreAuthenticate = True
                                .Method = "GET"

                            End With
                            Dim response As System.Net.HttpWebResponse = r.GetResponse()
                            If response.StatusCode = System.Net.HttpStatusCode.OK Then
                                Dim responseStream As Stream = response.GetResponseStream()
                                Dim document = XDocument.Load(responseStream)
                                Dim df As XNamespace = document.Root.Name.Namespace
                                Dim Call_State As String = document.Root.Element(df + "Call_State").Value
                                Dim Registration_State As String = document.Root.Element(df + "Registration_State").Value

                                If Table.Rows.Item(0).Item("_Profile_ID").ToString.Length = 0 Then
                                    'If there's no profile assigned to the device then show green circle.
                                    'This would be the expected behaviour
                                    If Registration_State = "Not Registered" Then
                                        Div.Attributes.Add("class", "status-circle ok")
                                        Div.Attributes.Add("title", "Device online, but no profile assigned")
                                    End If
                                Else
                                    'If we have a profile...
                                    If Registration_State = "Not Registered" Then 'We've got a profile but the extension isn't registered, this is bad show red circle
                                        Div.Attributes.Add("class", "status-circle bad")
                                        Div.Attributes.Add("title", "Device online but not registered")
                                    ElseIf Registration_State = "Registered" Then 'We've got a profile and profile is online and working correctly.
                                        Div.Attributes.Add("class", "status-circle ok")
                                        Div.Attributes.Add("title", "Device Registered and Online")
                                    ElseIf Registration_State.Contains("Failed") Then 'If any of the registration status contains the word 'Failed' then we've got a problem. (Normally, caused by wrong user details or Gradwell issues)
                                        Div.Attributes.Add("class", "status-circle bad")
                                        Div.Attributes.Add("title", "Attention Required")
                                        row.Attributes.Add("class", "warning-row") ' Highlight the row
                                    End If
                                    If Call_State = "Dialing" Or Call_State = "Connected Party Ringing" Or Call_State = "Connected" Then ' This overwrites previous rules as the phone has been detected as either dialing, ringing a number or connected this changes the status to blue
                                        Div.Attributes.Add("class", "status-circle incall")
                                        Div.Attributes.Add("title", "In Call")
                                    End If
                                End If
                                ' Unable to connect to remote server
                            Else
                                Throw New Exception("Error " & response.StatusCode)
                            End If
                        Catch ex As WebException
                            If ex.Status = WebExceptionStatus.ConnectFailure Then
                                Div.Attributes.Add("class", "status-circle offline")
                                Div.Attributes.Add("title", "Device not found")
                            ElseIf ex.Status = WebExceptionStatus.ProtocolError Then
                                Div.Attributes.Add("class", "status-circle offline")
                                Div.Attributes.Add("title", "Device not found")
                            End If
                        End Try

                    End Using
                End Using
            End Using
        Next
    End Sub

Thanks


Viewing all articles
Browse latest Browse all 4850

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>