more G-Labs products

Author Topic: C# script to shutdown XBMC  (Read 1536 times)

October 14, 2014, 11:36:38 PM
Read 1536 times

NicoVermeir

  • ****
  • Information
  • Sr. Member
  • Posts: 122
    • My blog
just wanted to share a quick C# script for HG that sends the shutdown command to the XBMC api. This way, the device running XBMC will perform a clean shutdown

just fill in the data in the top variables (same data that you'd put into a remote control app for XBMC)

Code: [Select]
//switch off XBMC
//xbmc settings
string xbmcJsonPath = "http://yourXBMCServer/jsonrpc";
string xbmcUser = "username";
string xbmcPass = "password";

//prepare xbmc request
var request = System.Net.WebRequest.Create(new Uri(xbmcJsonPath));
request.Credentials = new System.Net.NetworkCredential(xbmcUser, xbmcPass);
request.ContentType = "application/json";
request.Method = "POST";
var postStream = request.GetRequestStream();
var jsonRequest = @"{""jsonrpc"": ""2.0"",  ""method"": ""System.Shutdown"",  ""params"": {},  ""id"": 8}";
byte[] postData = System.Text.Encoding.UTF8.GetBytes(jsonRequest);

// post request
postStream.Write(postData, 0, postData.Length);
postStream.Dispose();

// request result, this is required to execute the lazy method
request.GetResponse();

October 15, 2014, 07:13:42 AM
Reply #1

mvdarend

  • *****
  • Information
  • Hero Member
  • Posts: 431
Thanks for sharing!

Gene, how about a sub-forum for code snippets?