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