Created
September 25, 2013 06:04
-
-
Save PaulGreenwell/6695685 to your computer and use it in GitHub Desktop.
VB.Net example for extracting the content from AccountRight API SDK error messages
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Protected Sub OnError(uri As System.Uri, ex As System.Exception) | |
'Display thr formatted message | |
Select Case ex.GetType() | |
Case GetType(System.Net.WebException) | |
MessageBox.Show(FormatMessage(CType(ex, System.Net.WebException))) | |
Case GetType(MYOB.AccountRight.SDK.ApiCommunicationException) | |
MessageBox.Show(FormatMessage(CType(ex.InnerException, System.Net.WebException))) | |
Case GetType(MYOB.AccountRight.SDK.ApiOperationException) | |
MessageBox.Show(ex.Message) | |
Case Else | |
MessageBox.Show(ex.Message) | |
End Select | |
HideSpinner() | |
End Sub | |
Private Function FormatMessage(webEx As System.Net.WebException) As String | |
Dim responseText As New StringBuilder() | |
responseText.AppendLine(webEx.Message) | |
responseText.AppendLine() | |
' Call method 'GetResponseStream' to obtain stream associated with the response object | |
Dim response As WebResponse = webEx.Response | |
Dim ReceiveStream As Stream = response.GetResponseStream() | |
Dim encode As Encoding = System.Text.Encoding.GetEncoding("utf-8") | |
' Pipe the stream to a higher level stream reader with the required encoding format. | |
Dim readStream As New StreamReader(ReceiveStream, encode) | |
Dim read(256) As [Char] | |
' Read 256 charcters at a time . | |
Dim count As Integer = readStream.Read(read, 0, 256) | |
responseText.AppendLine("HTML...") | |
While count > 0 | |
' Dump the 256 characters on a string and display the string onto the console. | |
Dim str As New [String](read, 0, count) | |
responseText.Append(str) | |
count = readStream.Read(read, 0, 256) | |
End While | |
responseText.Append("") | |
' Release the resources of stream object. | |
readStream.Close() | |
' Release the resources of response object. | |
response.Close() | |
Return responseText.ToString | |
End Function |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment