Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.27 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4. using System.Net;
  5. using System.IO;
  6. using System.Text;
  7. using Newtonsoft.Json.Linq;
  8.  
  9. namespace XbmcJson
  10. {
  11.     public delegate void JsonClientResponseRecieved(RequestState requestState);
  12.     public delegate void JsonClientResponseParsed(object parsedResult);
  13.  
  14.     public class JsonClient
  15.     {
  16.         string _username;
  17.         string _password;
  18.         Uri _remoteUri;
  19.  
  20.         int _id;
  21.  
  22.         public JsonClient(string username, string password, Uri remoteUri)
  23.         {
  24.             _username = username;
  25.             _password = password;
  26.             _remoteUri = remoteUri;
  27.         }
  28.  
  29.         // Creates webclient, subscribes to the uploadcompleted event and also passes it our request state
  30.         public void GetData(string method, object args, JsonClientResponseRecieved internalCallbackFunction, JsonClientResponseParsed externalCallbackFunction)
  31.         {
  32.             HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(_remoteUri);
  33.             webRequest.AllowWriteStreamBuffering = true;
  34.             webRequest.Credentials = new System.Net.NetworkCredential(_username, _password);
  35.             webRequest.ContentType = "application/json";
  36.             webRequest.Method = "POST";
  37.             webRequest.KeepAlive = false;
  38.             webRequest.Timeout = 10000;
  39.  
  40.             RequestState requestState = new RequestState(internalCallbackFunction, externalCallbackFunction);
  41.             requestState.jsonRequest = BuildJson(method, args);
  42.             requestState.webRequest = webRequest;
  43.  
  44.             webRequest.BeginGetRequestStream(new AsyncCallback(getRequestStream), requestState);
  45.         }
  46.  
  47.         public void getRequestStream(IAsyncResult asynchronousResult)
  48.         {
  49.             RequestState requestState = (RequestState)asynchronousResult.AsyncState;
  50.             WebRequest webRequest = requestState.webRequest;
  51.            
  52.             try
  53.             {
  54.                 using (var requestStream = webRequest.EndGetRequestStream(asynchronousResult))
  55.                 {
  56.                     using (var requestStreamWriter = new StreamWriter(requestStream, Encoding.ASCII))
  57.                     {
  58.                         requestStreamWriter.Write(requestState.jsonRequest);
  59.                     }
  60.                 }
  61.                 webRequest.BeginGetResponse(new AsyncCallback(getResponseStream), requestState);
  62.             }
  63.             catch
  64.             {
  65.                 throw new WebException();
  66.             }
  67.         }
  68.  
  69.         public void getResponseStream(IAsyncResult asynchronousResult)
  70.         {
  71.             RequestState requestState = (RequestState)asynchronousResult.AsyncState;
  72.             WebRequest webRequest = requestState.webRequest;
  73.  
  74.             WebResponse webResponse = webRequest.EndGetResponse(asynchronousResult);
  75.  
  76.             using (var responseStream = webResponse.GetResponseStream())
  77.             {
  78.                 using (var responseStreamReader = new StreamReader(responseStream, Encoding.UTF8))
  79.                 {
  80.                     requestState.jsonResponse = OnResponse(JObject.Parse(responseStreamReader.ReadToEnd()));
  81.  
  82.                     if (requestState.internalCallbackFunction != null)
  83.                     {
  84.                         requestState.internalCallbackFunction.Invoke(requestState);
  85.                     }
  86.                 }
  87.             }
  88.         }
  89.  
  90.         public static object OnResponse(JObject JObjectResponse)
  91.         {
  92.             var members = JObjectResponse.Properties();
  93.  
  94.             foreach (var member in members)
  95.             {
  96.                 if (string.CompareOrdinal(member.Name, "error") == 0)
  97.                 {
  98.                     throw new Exception("We've got an error: " + member.Value);
  99.                 }
  100.                 else if (string.CompareOrdinal(member.Name, "result") == 0)
  101.                 {
  102.                     if (member.Value.HasValues == true)
  103.                     {
  104.                         return (JObject)member.Value;
  105.                     }
  106.                     else
  107.                     {
  108.                         if (member.Value.Type == JTokenType.Integer)
  109.                             return (int)member.Value.Value<JValue>();
  110.                         else if (member.Value.Type == JTokenType.Float)
  111.                             return (float)member.Value.Value<JValue>();
  112.                         else
  113.                             return (string)member.Value.Value<JValue>();
  114.                     }
  115.                 }
  116.             }
  117.             throw new Exception("Invalid JSON-RPC response. It contains neither a result nor error.");
  118.         }
  119.  
  120.         public string BuildJson(string method, object args)
  121.         {
  122.             if (method == null)
  123.                 throw new ArgumentNullException("method");
  124.             if (method.Length == 0)
  125.                 throw new ArgumentException(null, "method");
  126.  
  127.             if (_id > 100000)
  128.                 _id = 0;
  129.  
  130.             var call = new JObject();
  131.             call.Add(new JProperty("jsonrpc", "2.0"));
  132.             call.Add(new JProperty("method", method));
  133.             if (args != null)
  134.                 call.Add(new JProperty("params", args));
  135.             call.Add(new JProperty("id", ++_id));
  136.  
  137.             return call.ToString();
  138.         }
  139.     }
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement