如何在java上使用带curl的json rpc

如何在java上使用带curl的json rpc,curl,ethereum,json-rpc,Curl,Ethereum,Json Rpc,我在本地计算机上设置了专用以太坊并以 geth --bootnodes="enode://b115ff8b97f67a6bd8294a4ea277930bf7825e755705e809442885aba85e397313e46528fb662a3828cd4356f600c10599b77822ebd192199b6e5b8cfdb530c4@127.0.0.1:30303" --networkid 15 console --datadir "private-data" --rpcport "

我在本地计算机上设置了专用以太坊并以

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
然后我在这里连接远程计算机的区块链节点

我想在java上使用带有curl的以太坊json rpc

我把它编码为

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();
}
}
这必须返回此行

{"jsonrpc":"2.0","id":1,"result":true}
但这个错误是错误的

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
这是命令

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'
它可以在终端上运行,但不能在java上运行


如果你能帮助我,真的很感激!!谢谢你用你的方式引用不同的部分,所以试着自己拆分命令行参数。此代码适用于:

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

(也就是说,只从Java发出HTTP请求要好得多。有很多资源可供使用。)

在终端运行的命令中,您在数据周围使用单引号(以免与JSON中的双引号冲突),但在Java代码中,你只是在两个地方都使用了双引号。顺便说一句,为什么你要使用
curl
而不仅仅是从Java发出web请求呢?我都做了。双引号和单引号。但它不起作用,并给出相同的错误消息。我认为有一些库可以在java上使用json rpc和curl。但是我找不到它