Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- using System.Collections.Generic;
- using System.Net;
- using System.IO;
- using System.Text;
- using Newtonsoft.Json.Linq;
- namespace XbmcJson
- {
- public delegate void JsonClientResponseRecieved(RequestState requestState);
- public delegate void JsonClientResponseParsed(object parsedResult);
- public class JsonClient
- {
- string _username;
- string _password;
- Uri _remoteUri;
- int _id;
- public JsonClient(string username, string password, Uri remoteUri)
- {
- _username = username;
- _password = password;
- _remoteUri = remoteUri;
- }
- // Creates webclient, subscribes to the uploadcompleted event and also passes it our request state
- public void GetData(string method, object args, JsonClientResponseRecieved internalCallbackFunction, JsonClientResponseParsed externalCallbackFunction)
- {
- HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(_remoteUri);
- webRequest.AllowWriteStreamBuffering = true;
- webRequest.Credentials = new System.Net.NetworkCredential(_username, _password);
- webRequest.ContentType = "application/json";
- webRequest.Method = "POST";
- webRequest.KeepAlive = false;
- webRequest.Timeout = 10000;
- RequestState requestState = new RequestState(internalCallbackFunction, externalCallbackFunction);
- requestState.jsonRequest = BuildJson(method, args);
- requestState.webRequest = webRequest;
- webRequest.BeginGetRequestStream(new AsyncCallback(getRequestStream), requestState);
- }
- public void getRequestStream(IAsyncResult asynchronousResult)
- {
- RequestState requestState = (RequestState)asynchronousResult.AsyncState;
- WebRequest webRequest = requestState.webRequest;
- try
- {
- using (var requestStream = webRequest.EndGetRequestStream(asynchronousResult))
- {
- using (var requestStreamWriter = new StreamWriter(requestStream, Encoding.ASCII))
- {
- requestStreamWriter.Write(requestState.jsonRequest);
- }
- }
- webRequest.BeginGetResponse(new AsyncCallback(getResponseStream), requestState);
- }
- catch
- {
- throw new WebException();
- }
- }
- public void getResponseStream(IAsyncResult asynchronousResult)
- {
- RequestState requestState = (RequestState)asynchronousResult.AsyncState;
- WebRequest webRequest = requestState.webRequest;
- WebResponse webResponse = webRequest.EndGetResponse(asynchronousResult);
- using (var responseStream = webResponse.GetResponseStream())
- {
- using (var responseStreamReader = new StreamReader(responseStream, Encoding.UTF8))
- {
- requestState.jsonResponse = OnResponse(JObject.Parse(responseStreamReader.ReadToEnd()));
- if (requestState.internalCallbackFunction != null)
- {
- requestState.internalCallbackFunction.Invoke(requestState);
- }
- }
- }
- }
- public static object OnResponse(JObject JObjectResponse)
- {
- var members = JObjectResponse.Properties();
- foreach (var member in members)
- {
- if (string.CompareOrdinal(member.Name, "error") == 0)
- {
- throw new Exception("We've got an error: " + member.Value);
- }
- else if (string.CompareOrdinal(member.Name, "result") == 0)
- {
- if (member.Value.HasValues == true)
- {
- return (JObject)member.Value;
- }
- else
- {
- if (member.Value.Type == JTokenType.Integer)
- return (int)member.Value.Value<JValue>();
- else if (member.Value.Type == JTokenType.Float)
- return (float)member.Value.Value<JValue>();
- else
- return (string)member.Value.Value<JValue>();
- }
- }
- }
- throw new Exception("Invalid JSON-RPC response. It contains neither a result nor error.");
- }
- public string BuildJson(string method, object args)
- {
- if (method == null)
- throw new ArgumentNullException("method");
- if (method.Length == 0)
- throw new ArgumentException(null, "method");
- if (_id > 100000)
- _id = 0;
- var call = new JObject();
- call.Add(new JProperty("jsonrpc", "2.0"));
- call.Add(new JProperty("method", method));
- if (args != null)
- call.Add(new JProperty("params", args));
- call.Add(new JProperty("id", ++_id));
- return call.ToString();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement