Skip to content

Instantly share code, notes, and snippets.

@atifaziz
Created September 13, 2008 10:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save atifaziz/10588 to your computer and use it in GitHub Desktop.
Save atifaziz/10588 to your computer and use it in GitHub Desktop.
Imports System
Imports System.Collections
Imports System.Web.Services.Protocols
Imports System.Net
Imports System.IO
Imports System.Text
Imports Microsoft.VisualBasic
Imports Jayrock.Json
Module MainModule
Sub Main()
Dim client As New JsonRpcClient()
client.Url = "http://www.raboof.com/projects/jayrock/demo.ashx"
Console.WriteLine(client.Invoke("system.about"))
Console.WriteLine(client.Invoke("system.version"))
Console.WriteLine(String.Join(Environment.NewLine, _
CType(New ArrayList(CType(client.Invoke("system.listMethods"), ICollection)).ToArray(GetType(String)), String())))
Console.WriteLine(client.Invoke("now"))
Console.WriteLine(client.Invoke("sum", 123, 456))
Console.WriteLine(client.Invoke("total", New Integer() {1, 2, 3, 4, 5, 6, 7, 8, 9}))
client.CookieContainer = New CookieContainer()
Console.WriteLine(client.Invoke("counter"))
Console.WriteLine(client.Invoke("counter"))
End Sub
Class JsonRpcClient
Inherits HttpWebClientProtocol
Private _id As Integer
Public Overridable Function Invoke(ByVal method As String, ByVal ParamArray args() As Object) As Object
Dim request As WebRequest = GetWebRequest(New Uri(Url))
request.Method = "POST"
Using stream As Stream = request.GetRequestStream()
Using writer As StreamWriter = New StreamWriter(stream, Encoding.UTF8)
Dim callObject As JsonObject = New JsonObject
_id += 1
callObject("id") = _id
callObject("method") = method
callObject("params") = args
callObject.Export(New JsonTextWriter(writer))
End Using
End Using
Using response As WebResponse = GetWebResponse(request)
Using stream As Stream = response.GetResponseStream()
Using reader As StreamReader = New StreamReader(stream, Encoding.UTF8)
Dim answer As JsonObject = New JsonObject
answer.Import(New JsonTextReader(reader))
Dim errorObject As Object = answer("error")
If Not errorObject Is Nothing Then
OnError(errorObject)
End If
Return answer("result")
End Using
End Using
End Using
End Function
Protected Overridable Sub OnError(ByVal errorObject As Object)
Dim err As JsonObject = TryCast(errorObject, JsonObject)
If Not err Is Nothing Then
Throw New Exception(TryCast(err("message"), String))
End If
Throw New Exception(TryCast(errorObject, String))
End Sub
End Class
End Module
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment