Witam,
Mam kawałek kodu w jęzku C#:
**public JObject InvokeMethod(string a_sMethod, params object[] a_params)
{
var perso = JsonConvert.DeserializeObject<dynamic>(a_sMethod);
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(Url);
webRequest.Credentials = Credentials;
webRequest.ContentType = "application/json-rpc";
webRequest.Method = "POST";
JObject joe = new JObject();
joe["jsonrpc"] = "1.0";
joe["id"] = "1";
joe["method"] = perso;
if (a_params != null)
{
if (a_params.Length > 0)
{
JArray props = new JArray();
foreach (var p in a_params)
{
props.Add(p);
}
joe.Add(new JProperty("params", props));
}
}
string s = JsonConvert.SerializeObject(joe);
// serialize json for the request
byte[] byteArray = Encoding.UTF8.GetBytes(s);
webRequest.ContentLength = byteArray.Length;
try
{
using (Stream dataStream = webRequest.GetRequestStream())
{
dataStream.Write(byteArray, 0, byteArray.Length);
}
}
catch (WebException we)
{
throw new Exception("JSON rpc does not work");
}
WebResponse webResponse = null;
try
{
using (webResponse = webRequest.GetResponse())
{
using (Stream str = webResponse.GetResponseStream())
{
using (StreamReader sr = new StreamReader(str))
{
return JsonConvert.DeserializeObject<JObject>(sr.ReadToEnd());
}
}
}
}
catch (WebException webex)
{
using (Stream str = webex.Response.GetResponseStream())
{
using (StreamReader sr = new StreamReader(str))
{
var tempRet = JsonConvert.DeserializeObject<JObject>(sr.ReadToEnd());
return tempRet;
}
}
}
catch (Exception)
{
throw new Exception("");
}**
Chcę go wywołać w innej klasie w frameworku NUnit. Metodę, którą mam jest typu bool, powiedzmy wygląda to tak:
**protected override bool Test()
{
log.Writer("Test1");
Websocket ws = new Websocket();
ws.InvokeMethod();
}**
Potrzebuje użycia tych dwóch parametrów : string a_sMethod i object a_params. Dodam tylko, że {get; set;} nie zadziałał, dostałem informację value cannot be null. Jeśli jednak zostawię puste InvokeMethod() dostaję błąd treści: There is no argument that correspondsto the required formal parameter 'a_sMethod' .
Dodam, że potrzebuje json rpc aby dostać dane z websocketu jak params, token, id, position. Podobnie jak w Dev Tools Chrome> Network> WS.