JSON-RPC for Java 访问bitcoind服务

本文介绍了如何配置bitcoin.conf以启用RPC服务,并使用Java的JSON-RPC库进行Maven集成,从而访问和操作bitcoind节点。
摘要由CSDN通过智能技术生成

1.bitcoin.conf配置:

server=1
 rpcuser=name
 rpcpassword=pwd
 rpctimeout=30
 rpcport=8332

2.json-rpc for java maven

<dependency>
        <groupId>com.github.briandilley.jsonrpc4j</groupId>
        <artifactId>jsonrpc4j</artifactId>
        <version>1.1</version>
    </dependency>

3.代码示例:

public class TestRpc {
	public static void main(String[] args) throws Throwable{
		String cred = Base64.encodeBytes(("name" + ":" +"pwd").getBytes());
		Map<String, String> headers = new HashMap<String, String>(1);
		//身份认证
		headers.put("Authorization", "Basic " + cred);
		JsonRpcHttpClient client = new JsonRpcHttpClient(

	             new URL("http://localhost:8332"),headers);
       String result = (String) client.invoke("getblockhash",new Object[]{1},Object.class);
     
	        System.out.println(result);
	}
}


获取高度为1的block的hash值返回00000000839a8e6886ab5951d76f411475428afc90947ee320161bbf18eb6048

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值