How do I connect to a multichain I set up, using JSoup?

+2 votes

I have the following method, which maps parameters to a JSON String:

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 + "}";
        }

And then this method to get the information from the blockchain

  public static void getBlockChainParams() throws IOException
        {
            // params
            String server = "192.168.0.0";
            String port = "0000";
            String chainName = "iTextChain";
            
            // 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());        
        }

This code keeps throwing SocketException however.
What am I doing wrong?

asked Jan 10, 2017 by Joris Schellekens

1 Answer

+1 vote

The problem may be that MultiChain is not accepting incoming API connections from your code, if your code is running on a different computer. Please search this Q&A for rpcallowip to see solutions.

answered Jan 12, 2017 by MultiChain
...