How to consume JSON RO Web services?

(Wilson) #1


refer to the document

i manage to consume the soap services, would like to check how about the JSON ? i have add in the JSON message type in to server channel. Below is my test in client program:

protected void btnSoap_Click(object sender, EventArgs e)
System.ServiceModel.Channels.Binding b = (System.ServiceModel.BasicHttpBinding)Activator.CreateInstance(typeof(System.ServiceModel.BasicHttpBinding));
System.ServiceModel.EndpointAddress eAddress = new System.ServiceModel.EndpointAddress(“http://localhost:8099/soap”);

        soapServices.ROServer1ServiceClient iTest = new soapServices.ROServer1ServiceClient(b, eAddress);

        var msgHeader = new soapServices.ROClientIDHeader();
        msgHeader.ID = String.Concat("{", Guid.NewGuid().ToString(), "}");

        var result = iTest.GetServerTime(ref msgHeader);
        txtResultSOAP.Text = result.ToString();


(antonk) #2

You’d need to compose a requiest JSON string, send it to the server at http://[servername]:[serverport]/json (using .NET WebClient or similar class), then get back a response JSON string and parse it. can help you to generate .NET classes that match the provided JSON string.
Requested JSON string formats can be viewed using Fiddler: create RO SDK client and server and inspect the traffic between them using this awesome tool.

(Wilson) #3

Hi, we use the sample server, JSON message, when i consume the services,


should expect below result


but it return error as below:

Server Error

The following error occurred:

500 Invalid path specified: ‘/json/sum?a=1&b=2’.

at RemObjects.SDK.Server.IpHttpServerChannel.HandleHttpRequest(Object sender, HttpRequestEventArgs e)
Framework Version v4.0.30319.34209
RemObjects SDK for .NET v8.3.91.1167
Internet Pack for .NET v8.3.91.1167

RemObjects Software, LLC.

(antonk) #4

Requests where parameters are passed via URL are not supported. You need to pass the request JSON string via request body, not its Url

(Wilson) #5

Hi, i have done some research and most of the way is using URL. Anywhere, i have try to code as below to get the JSON data back, but the server return error. Or might be can you give some code to show how it work ?

    protected void btnServerDateTimeJSON_Click(object sender, EventArgs e)
        string urlPath = "http://localhost:8099/json/SUM";

        string data = "A=11&B=22"; //replace <value>
        byte[] dataStream = Encoding.UTF8.GetBytes(data);
        WebRequest webRequest = WebRequest.Create(urlPath);
        webRequest.Method = "POST";
        webRequest.ContentType = "application/x-www-form-urlencoded";
        webRequest.ContentLength = dataStream.Length;  
        Stream newStream = webRequest.GetRequestStream();
        // Send the data.
        newStream.Write(dataStream, 0, dataStream.Length);
        using (var httpWebResponse = webRequest.GetResponse() as HttpWebResponse)
            if (httpWebResponse != null)
                using (var streamReader = new StreamReader(httpWebResponse.GetResponseStream()))
                    txtResultJSON.Text = streamReader.ReadToEnd();

(antonk) #6

Server expexts a JSON string in the request body. Like f.e.


Then response will be, f.e.


(Wilson) #7

the correct JSON response from server is


for those interested on .net client to read it, you can refer to below code:

c# .net
var jsonResult = new JavaScriptSerializer().Deserialize(result);
txtResultJSONGetSum.Text = (jsonResult[“result”][“Result”]).ToString();


(antonk) #8

This is the JSON message version. Assume it always be 1.1

This is the remote session Id. Can be useful for authorization tasks.

Data payload (server method result).

One can use tools like to convert JSON strings into corresponding C# data structures.

(DonaldShimoda) #9

Any way to get that working as a standard json call?


Best regards

(antonk) #10

Yes. You need to use HttpAPI:

(EvgenyK) #11

another way:
you can transform this string into valid json request in HTTPServer.OnCustomResponseEvent event.

see more at Using OnCustomResponseEvent in a ROSDK Server snippet