服务器提交了协议违规。 Section = ResponseHeader Detail ='Content-Length'标头值无效

时间:2018-03-01 11:13:58

标签: c# bitcoin json-rpc

当我尝试使用C#与wallet-qt进行通信时,我收到此错误并且不知道我错过了什么。我也尝试了很多谷歌的解决方案,但没有成功请帮助我谢谢。 这是我的代码,我从这个主题得到了一个想法:https://bitcoin.stackexchange.com/questions/5810/how-do-i-call-json-rpc-api-using-c/5811#5811?newreg=e26b1cb9e4b24ebdac98193efb5d3f4b

  static void Main(string[] args)
    {
        var ret = InvokeMethod("getblockhash", 15);
    }

    public static JObject InvokeMethod(string a_sMethod, params object[] a_params)
    {
        HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("http://localhost:9998");
        webRequest.Credentials = new NetworkCredential("user", "pass"); 

        webRequest.ContentType = "application/json-rpc";
        webRequest.Method = "POST";

        JObject joe = new JObject();
        joe["jsonrpc"] = "1.0";
        joe["id"] = "1";
        joe["method"] = a_sMethod;

        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)
        {
            //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;
        }
        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;
        }
    }

0 个答案:

没有答案