在JSON-RPC中如果运行下面的源代码,它将显示错误或NaN结果,它不会返回JSON对象字符串作为响应。
我已手动创建了一个类并设置了id
和command
,然后对其进行了序列化,但我不确定为什么会出现错误我有很多更改,但没有得到好的结果。< / p>
我不确定我忘记了什么,并且正在努力让它按预期工作。
我的预期结果是:{ "id": 1, "status": "success", "type": "response", "result": {} }
namespace JsonRpcRipple
{
public class RipTest
{
public int id { get; set; }
public string command { get; set; }
}
public partial class Default : System.Web.UI.Page
{
public JObject InvokeMethod()
{
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("http://s1.ripple.com:51234/");
webRequest.ContentType = "application/json-rpc";
webRequest.Method = "POST";
RipTest joe = new RipTest();
joe.id = 1;
joe.command = "ping";
string s = JsonConvert.SerializeObject(joe);
// serialize json for the requestf
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)
{
//inner exception is socket
//{"A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 23.23.246.5:8332"}
throw we;
}
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;
}
}
protected void Page_Load(object sender, EventArgs e)
{
var ret = InvokeMethod();
Response.Write(ret);
}
}
}