使用JSoup从Multichain API获取getinfo

时间:2017-01-10 15:02:15

标签: java json jsoup blockchain

我试图在现有的多链(我管理的)上调用方法“getInfo”,使用JSoup库来设置数据。

以下方法使用JSON编码参数:

 private static String encode(Map<String, String> params)
    {
        String retval = "";        
        for(Entry<String,String> en : params.entrySet())
        {
            retval += (retval.isEmpty() ? "" : ", ") + "\"" + en.getKey() + "\" : \"" + en.getValue() + "\"";
        }
        retval += ", \"jsonrpc\" : \"1.0\"";
        retval += ", \"params\" : []";
        return "{" + retval + "}";
    }

然后,此方法尝试调用我设置的Multichain API:

public static void getBlockChainParams() throws IOException
    {
        // params
        String server = "192.168.00.00"; // obviously not the real IP
        String port = "0000"; // obviously not the real port
        String chainName = "chain1";

        // json
        Map<String,String> params = new HashMap<>();
        params.put("method", "getinfo");
        params.put("id", "1");
        params.put("chain_name", chainName);

        String jsonData = encode(params);
        System.out.println(jsonData);

        // build request
        String url = "http://" + server + ":" + port;
        System.out.println(url);
        Document doc = Jsoup.connect(url)
            .ignoreContentType(true)
            .ignoreHttpErrors(true)             
            .header("Accept", "application/json")
            //.header("Accept-Encoding", "gzip,deflate,sdch")
            //.header("Accept-Language", "en-EN,es;q=0.8")
            .header("Connection", "keep-alive")
            //.header("X-Requested-With", "XMLHttpRequest")
            .data(jsonData, "")
            .post();

        // send 
       System.out.println(doc.toString());        
    }

但是,当我运行此代码时,它会抛出一个SocketException。

我尝试(大致)使用相同的代码连接到https://jsonplaceholder.typicode.com/posts并为其提供一些JSON数据,这些数据完美无缺(给我一个包含我发送的数据的简单HTML文档)。

1 个答案:

答案 0 :(得分:-2)

对多个人的双重引用应该被传递为&#34; \