예제 #1
0
        public async void getDevices()
        {
            json.Value postData = new json.ObjectValue();
            postData["_sid"]           = sid;
            postData["limit"]          = "1000";
            postData["api"]            = "SYNO.AudioStation.Folder";
            postData["method"]         = "list";
            postData["library"]        = "shared";
            postData["additional"]     = "song_tag,song_audio,song_rating";
            postData["sort_by"]        = "track";
            postData["sort_direction"] = "ASC";
            postData["version"]        = "2";
            json.Value v = await callApi("POST", "AudioStation/remote_player.cgi", "?_sid=" + sid, postData);

            Console.WriteLine(v.ToString());
        }
예제 #2
0
        private async Task <dynamic> callApi(string method, string api, string parameters, json.Value postData = null)
        {
            HttpClient client = new HttpClient();

            client.BaseAddress = new Uri("http://192.168.2.205:5000/webapi/" + api);
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            // List data response.
            HttpResponseMessage response = null;

            if (method == "POST")
            {
                Dictionary <string, string> postFields = new Dictionary <string, string>();
                foreach (var a in postData)
                {
                    postFields[a.Key] = a.Value.asString();
                }
                response = client.PostAsync(parameters, new FormUrlEncodedContent(postFields)).Result;                  // Blocking call!
                //response = client.PostAsync(parameters, new StringContent(postData.ToString(), Encoding.UTF8, "application/json")).Result;  // Blocking call!
            }
            else
            {
                response = client.GetAsync(parameters).Result;                  // Blocking call!
            }
            if (response.IsSuccessStatusCode)
            {
                var data = Encoding.UTF8.GetString(await response.Content.ReadAsByteArrayAsync());
                //	Console.WriteLine("API OK: " + data);
                //return json.Value.fromString(data);
                return(JsonConvert.DeserializeObject <dynamic>(data));
            }
            else
            {
                Console.WriteLine("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase);
            }
            return(null);
        }