Example of calling a JSON web service from a C# application

5,340 views
Skip to first unread message

John Juul Jensen

unread,
Nov 26, 2006, 6:20:06 PM11/26/06
to jay...@googlegroups.com
I just created a small example of how to call a json service from a C#
program. It doesn't do the whole "create typed proxy class" thing, but
rather uses a generic service wrapper, which currently supports methods
with up to 4 parameters. I've attached the program and the test service.

Enjoy
John

Program.cs
TestService.ashx

Atif Aziz

unread,
Nov 26, 2006, 6:41:20 PM11/26/06
to jay...@googlegroups.com
Hey John,

I also worked on a prototype last Friday that works across all framework
versions. I've attached it for your use/review. A client-side proxy can
also be implemented on top of this by inheriting and adding service
methods that internally delegate to base's Invoke. Only synchronous
calls are supported for now.

Cheers,
Atif

Program.cs
JsonRpcClient.cs

John Juul Jensen

unread,
Nov 27, 2006, 1:18:22 AM11/27/06
to jay...@googlegroups.com
Very nice and I never thought about doing this:
using (WebResponse response = GetWebResponse(request))
using (Stream stream = response.GetResponseStream())
using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))

very clever.

Ghost

unread,
Jan 19, 2007, 11:09:48 AM1/19/07
to Jayrock
Hi Guys,

I'm trying to use C# with Jayrock in the client side and Json-rpc with
php in the server side, I just tried to run your example, of course I
change the url and the method, but always I have:

{"response":"\"The method you requested, , was not
found.\"","error":"1","id":null}

I tested the web services and this is working good.

Atif Aziz

unread,
Jan 19, 2007, 1:20:35 PM1/19/07
to jay...@googlegroups.com
Have you been able to make your PHP JSON-RPC service work against another client like from JavaScript? I would suggest using a tool like Fiddler (http://www.fiddlertool.com/) to look at the HTTP requests and responses exchanged and compare those with a working version. The only thing I can think of that the C# client does not do is set the Content-Type header and perhaps your PHP implementation of JSON-RPC is sensitive to that? I doubted somehow because it looks like it is not receiving the method name (it is blank).

- Atif

Ghost

unread,
Jan 20, 2007, 7:59:13 AM1/20/07
to Jayrock
Thanks, the problem was the header, specifically: Content-Length, I
wrote something like:

System.Net.Sockets.Socket s = ConnectSocket("localhost", 80);
// my method
string txt = "{\"method\": \"mymethod\" ,\"params\":
{\"user\":\"me\",\"password\":\"pass\"},\"id\":8}";

System.Text.StringBuilder sb = new
System.Text.StringBuilder();
sb.Append("POST /myapp/server.php HTTP/1.0\r\n");
sb.Append("Host: localhost\r\n}");
sb.Append("User-Agent: myJsonApp\r\n");
sb.Append("Connection: close\r\n");
sb.Append("Content-Type: application/json\r\n");
sb.Append("Content-Length: " + txt.Length.ToString() +
"\r\n");
sb.Append("Accept: application/json\r\n");
sb.Append("\r\n");
sb.Append(txt);
byte[] send =
System.Text.Encoding.UTF8.GetBytes(sb.ToString());
s.Send(send);

This works, but, How I can do the same in JsonRpcClient.Invoke, I don't
know how to set the content-length.

Atif Aziz

unread,
Jan 20, 2007, 4:26:06 PM1/20/07
to jay...@googlegroups.com
There is a ContentLength property on the WebRequest object that is created at start of the Invoke method.

Ghost

unread,
Jan 22, 2007, 9:36:46 AM1/22/07
to Jayrock
but what value? JsonObject length?

Atif Aziz

unread,
Jan 22, 2007, 9:52:58 AM1/22/07
to jay...@googlegroups.com
The simplest way would be to convert the JsonObject into a byte array holding the JSON text in UTF-8 and then use its length to set ContentLength on the WebRequest object. So you could modify the code like this...

JsonObject call = new JsonObject();
call["id"] = ++_id;
call["method"] = method;
call["params"] = args;
string callString = JsonConvert.ExportToString(call);
byte[] callBytes = Encoding.UTF8.GetBytes(callString);

WebRequest request = GetWebRequest(new Uri(Url));
request.Method = "POST";
request.ContentType = "application/json; charset=utf-8";
request.ContentLength = callBytes.Length;

using (Stream stream = request.GetRequestStream())
stream.Write(callBytes, 0, callBytes.Length);

I haven't compiled this so it may contain an error or two, but you get the general idea.

Reply all
Reply to author
Forward
0 new messages