how to use json rpc with curl on java

霸气de小男生 提交于 2019-12-25 03:24:29

问题


i set the private ethereum on my local computer and run as

geth --bootnodes="enode://b115ff8b97f67a6bd8294a4ea277930bf7825e755705e809442885aba85e397313e46528fb662a3828cd4356f600c10599b77822ebd192199b6e5b8cfdb530c4@127.0.0.1:30303" --networkid 15 console --datadir "private-data" --rpcport "8545" --rpc --rpccorsdomain "*" --rpcapi "eth,web3,personal" --rpcaddr 192.168.44.114

and then i connect here with the remote computer's blockchain nodes

i want to use ethereum json rpc with curl on java .

i coded it as

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

public class shell{
public static void makeTran() throws Exception {


String shellcmd = "curl -X POST --data \"{\"jsonrpc\":\"2.0\",\"method\":\"personal_unlockAccount\",\"params\":[\"0xc7d863e8c89ac4b0336059b4e2cf84a57a6ba7db\", \"1\", 10],\"id\":1}\" http://192.168.44.114:8545/ -H \"Content-Type: application/json\"";
System.out.println(shellcmd);

Process process = Runtime.getRuntime().exec(shellcmd);

InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
while ((line = br.readLine()) != null) {
    System.out.println(line);
}

}
    public static void main(String[] args) throws Exception {
    makeTran();
}
}

this must return this line

{"jsonrpc":"2.0","id":1,"result":true}

but this error is

curl -X POST --data "{"jsonrpc":"2.0","method":"personal_unlockAccount","params":["0xc7d863e8c89ac4b0336059b4e2cf84a57a6ba7db", "1", 10],"id":1}" http://192.168.44.114:8545/ -H "Content-Type: application/json"
invalid content type, only application/json is supported

here is command

curl -X POST --data '{"jsonrpc":"2.0","method":"personal_unlockAccount","params":["0xc7d863e8c89ac4b0336059b4e2cf84a57a6ba7db", "1", 10],"id":1}' http://192.168.44.114:8545/ -H 'Content-Type: application/json'

it can be run on terminal, but its not work on java

if you help me , really appreciate of it !! thanks


回答1:


Quoting different pieces the way you're doing doesn't work, so try splitting up the command-line arguments yourself. This code works:

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

public class Test {
    public static void main(String[] args) throws Exception {
        Process process = Runtime.getRuntime().exec(new String[] {
            "curl",
            "-H",
            "Content-Type: application/json",
            "--data",
            "{\"jsonrpc\":\"2.0\",\"method\":\"eth_blockNumber\",\"id\":1}",
            "https://mainnet.infura.io/",
        });

        InputStream is = process.getInputStream();
        InputStreamReader isr = new InputStreamReader(is);
        BufferedReader br = new BufferedReader(isr);
        String line;

        while ((line = br.readLine()) != null) {
            System.out.println(line);
        }
    }
}

(That said, it's much better to just make the HTTP request from Java. There are plenty of resources available for how to do that.)



来源:https://stackoverflow.com/questions/51823534/how-to-use-json-rpc-with-curl-on-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!