call JSON RPC API Using C#?

call JSON RPC API Using C#?

I am trying to call JSON RPS API Using C#. I have added my own IP address with its credentials.

Before writing API in C#, I have installed Bitcoin on my server. Now, using the server details, i would like to test the API whether it is working or not.

Here is the program.cs file.

public static void Main(string[] args)
    {
        var data = RequestServer("getaccount", new List<string>() { "value"});
        Console.WriteLine(data);
        Console.ReadKey();
    }

    public static async Task<string> RequestServer(string methodName, List<string> parameters)
    {

        string ServerIp = "http://***.***.**.***:8333";
        string UserName = "root";
        string Password = "********";
        HttpWebRequest webRequest = HttpWebRequest.CreateHttp(ServerIp);
        webRequest.Credentials = new NetworkCredential(UserName, Password);

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

        HttpClient cli = new HttpClient();
        cli.DefaultRequestHeaders.ExpectContinue = false;

        string respVal = string.Empty;

        JObject joe = new JObject();
        joe.Add(new JProperty("jsonrpc", "1.0"));
        joe.Add(new JProperty("id", "1"));
        joe.Add(new JProperty("method", methodName));

        JArray props = new JArray();
        foreach (var parameter in parameters)
        {
            props.Add(parameter);
        }

        joe.Add(new JProperty("params", props));

        // serialize json for the request
        string s = JsonConvert.SerializeObject(joe);
        byte[] byteArray = Encoding.UTF8.GetBytes(s);
        Stream dataStream =await webRequest.GetRequestStreamAsync();
        dataStream.Write(byteArray, 0, byteArray.Length);
        dataStream.Dispose();

        StreamReader streamReader = null;
        try
        {
            HttpWebResponse webResponse = await webRequest.GetResponseAsync() as HttpWebResponse;

            streamReader = new StreamReader(webResponse.GetResponseStream(), true);

            respVal = streamReader.ReadToEnd();
            var data = JsonConvert.DeserializeObject(respVal).ToString();
            return data;
        }
        catch (Exception exp)
        {
            Console.WriteLine(exp.InnerException.Message);
        }
        finally
        {
            if (streamReader != null)
            {
                streamReader.Dispose();
            }

        }
        return string.Empty;
    }
}

Can any one help me to test the api using my server details please?

http://ift.tt/2F4Kbod

Comments

Popular posts from this blog

Bitcoin Core errors with database block

Bitfinex & Market Synergy To Offer Institutional Calibre Cryptocurrency Connectivity