JSON-RPC over HTTP?
#11
Here's my example in .NET:


Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Web;
using System.Web.Script.Serialization;

namespace XBMCLib
{
    public class XBMCHelper
    {
        private static string _ServerName = "http://localhost:9091/jsonrpc";

        private static string PostCommand(string istrCommand)
        {
            byte[] byteArray = Encoding.ASCII.GetBytes(istrCommand);

            WebRequest wreq = WebRequest.Create(_ServerName);
            wreq.Method = "POST";
            wreq.ContentLength = byteArray.Length;
            wreq.ContentType = "application/x-www-form-urlencoded";
            Stream dataStream = wreq.GetRequestStream ();
            dataStream.Write (byteArray, 0, byteArray.Length);
            dataStream.Close();

            WebResponse wresp = wreq.GetResponse();
            dataStream = wresp.GetResponseStream();
            StreamReader reader = new StreamReader (dataStream);
            // Read the content.
            string responseFromServer = reader.ReadToEnd();
            
            reader.Close();
            dataStream.Close();
            wresp.Close();

            return responseFromServer.Replace(" ", " ");
        }

        protected struct PingResp
        {
            public string id;
            public string jsonrpc;
            public string result;
        }

        public static bool XBMCPing()
        {
            PingResp res;

            string cmd = "{\"jsonrpc\": \"2.0\", \"method\": \"JSONRPC.Ping\", \"id\": \"1\"}";

            JavaScriptSerializer ser = new JavaScriptSerializer();
            res = ser.Deserialize<PingResp>(PostCommand(cmd));

            return res.result == "pong";
        }
    }
}
Reply


Messages In This Thread
JSON-RPC over HTTP? - by bradvido88 - 2010-12-20, 17:24
[No subject] - by _Andy_ - 2010-12-20, 17:59
[No subject] - by darkscout - 2010-12-20, 18:01
[No subject] - by bradvido88 - 2010-12-20, 18:17
[No subject] - by bradvido88 - 2010-12-20, 19:16
[No subject] - by Montellese - 2010-12-20, 20:42
[No subject] - by bradvido88 - 2010-12-20, 20:49
[No subject] - by darkscout - 2010-12-20, 21:51
[No subject] - by bradvido88 - 2010-12-20, 21:52
[No subject] - by plumser - 2010-12-21, 19:49
[No subject] - by Adam - 2011-01-01, 04:57
[No subject] - by Montellese - 2011-01-01, 11:46
[No subject] - by toenuff - 2011-01-04, 05:26
[No subject] - by toenuff - 2011-01-04, 05:45
[No subject] - by manxam - 2011-01-05, 09:17
[No subject] - by Montellese - 2011-01-05, 10:48
Actionscript 2.0 - by TheyKilledKenny - 2011-01-07, 13:18
[No subject] - by Montellese - 2011-01-07, 14:08
[No subject] - by TheyKilledKenny - 2011-01-07, 18:41
[No subject] - by Montellese - 2011-01-07, 20:27
[No subject] - by ArcticGiraffe - 2011-08-13, 02:08
[No subject] - by topfs2 - 2011-08-13, 10:40
[No subject] - by ArcticGiraffe - 2011-08-13, 15:32
[No subject] - by topfs2 - 2011-08-13, 18:48
[No subject] - by ArcticGiraffe - 2011-08-13, 19:27
RE: JSON-RPC over HTTP? - by Richard6360 - 2016-05-22, 11:38
Logout Mark Read Team Forum Stats Members Help
JSON-RPC over HTTP?1