public virtual object Invoke(string method, params object[] args) { WebRequest request = GetWebRequest(new Uri(Url)); request.Method = "POST"; using (Stream stream = request.GetRequestStream()) using (StreamWriter writer = new StreamWriter(stream, Encoding.UTF8)) { JsonObject call = new JsonObject(); call["id"] = ++_id; call["method"] = method; call["params"] = args; call.Export(new JsonTextWriter(writer)); } using (WebResponse response = GetWebResponse(request)) using (Stream stream = response.GetResponseStream()) using (StreamReader reader = new StreamReader(stream, Encoding.UTF8)) { JsonObject answer = new JsonObject(); answer.Import(new JsonTextReader(reader)); object errorObject = answer["error"]; if (errorObject != null) OnError(errorObject); return answer["result"]; } }
public virtual object Invoke(string method, params object[] args) { string action = method + "(" + Util.ArrayToStringGeneric(args, ", ") + ") "; //Console.WriteLine(action); return Util.RetryAction<object>(() => { WebRequest request = GetWebRequest(new Uri(Url)); request.Method = "POST"; request.Timeout = 45000; using (Stream stream = request.GetRequestStream()) using (StreamWriter writer = new StreamWriter(stream, Encoding.UTF8)) { JsonObject call = new JsonObject(); call["id"] = ++_id; call["method"] = method; call["params"] = args; call.Export(new JsonTextWriter(writer)); } using (HttpWebResponse response = (HttpWebResponse)GetWebResponse(request)) using (Stream stream = response.GetResponseStream()) using (StreamReader reader = new StreamReader(stream, Encoding.UTF8)) { JsonObject answer = new JsonObject(); if (response.StatusCode != HttpStatusCode.OK) { throw new Exception(action + "got Bad HTTP response: " + response.StatusCode); } else { answer.Import(new JsonTextReader(reader)); } object errorObject = answer["error"]; if (errorObject != null) OnError(errorObject); return answer["result"]; } }, action, 20, 100); // retry with delay throw new Exception(action + "failed, too many retries"); }
public void Export() { JsonObject o = new JsonObject(); o.Put("Number", 123); o.Put("String", "Hello World"); o.Put("Boolean", true); JsonRecorder writer = new JsonRecorder(); o.Export(writer); JsonReader reader = writer.CreatePlayer(); reader.ReadToken(JsonTokenClass.Object); string[] members = (string[]) o.GetNamesArray().ToArray(typeof(string)); Assert.AreEqual(members[0], reader.ReadMember()); Assert.AreEqual(o[members[0]], reader.ReadNumber().ToInt32()); Assert.AreEqual(members[1], reader.ReadMember()); Assert.AreEqual(o[members[1]], reader.ReadString()); Assert.AreEqual(members[2], reader.ReadMember()); Assert.AreEqual(o[members[2]], reader.ReadBoolean()); Assert.AreEqual(JsonTokenClass.EndObject, reader.TokenClass); }
public void Export() { JsonObject o = new JsonObject(); o.Put("Number", 123); o.Put("String", "Hello World"); o.Put("Boolean", true); JsonRecorder writer = new JsonRecorder(); o.Export(writer); JsonReader reader = writer.CreatePlayer(); reader.ReadToken(JsonTokenClass.Object); string[] members = (string[])o.GetNamesArray().ToArray(typeof(string)); Assert.AreEqual(members[0], reader.ReadMember()); Assert.AreEqual(o[members[0]], reader.ReadNumber().ToInt32()); Assert.AreEqual(members[1], reader.ReadMember()); Assert.AreEqual(o[members[1]], reader.ReadString()); Assert.AreEqual(members[2], reader.ReadMember()); Assert.AreEqual(o[members[2]], reader.ReadBoolean()); Assert.AreEqual(JsonTokenClass.EndObject, reader.TokenClass); }
public void CannotUseNullArgWithExport() { IJsonExportable o = new JsonObject(); o.Export(null, null); }
public JsonWsp.Response CallMethod(string methodname, JsonObject args, Dictionary<String, String> cookies = null, Dictionary<String, String> headers = null) { JsonObject req_dict = new JsonObject(); req_dict.Add("methodname",methodname); req_dict.Add("type","jsonwsp/request"); req_dict.Add("args",args); JsonWriter json_req_writer = new JsonTextWriter(); req_dict.Export(json_req_writer); Dictionary<String, String> cookieList = m_cookieList; if (cookies != null) { foreach (String key in cookies.Keys) { cookieList[key] = cookies[key]; } } Dictionary<String, String> headerList = m_headerList; if (headers != null) { foreach (String key in headers.Keys) { headerList[key] = headers[key]; } } string content_type = "application/json; charset=utf-8"; JsonWsp.Response jsonwsp_response = SendRequest(m_service_url, json_req_writer.ToString(),content_type, cookieList, headerList); // Convert response text return jsonwsp_response; }