Help! Connection to Mutlichain using Java Client?

+1 vote
How am I able to connect to Multichain block running on another server using a Java client? Thanks for the help
asked Aug 2, 2016 by Vern.S

1 Answer

+1 vote

You would need to create a simple JAVA RPC client and use the authentication header in the HTTP request to pass the authentication attributes.  Please find a sample below:

    public JSONObject invokeRPC(String id, String method, List<Object> params) {
        DefaultHttpClient httpclient = new DefaultHttpClient();
        JSONObject json = new JSONObject();
        json.put("id", id);
        json.put("chain_name", chain_name);
        json.put("method", method);
        if (null != params) {
            json.put("params", params);
        }
        JSONObject responseJsonObj = null;
        try {
            httpclient.getCredentialsProvider().setCredentials(new AuthScope(ip, port),
                    new UsernamePasswordCredentials(uName, pwd));
            StringEntity myEntity = new StringEntity(json.toJSONString());

            HttpPost httppost = new HttpPost("http://"+ip+":"+port);
            httppost.setEntity(myEntity);
            
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();

            System.out.println("----------------------------------------");
            System.out.println(response.getStatusLine());
            if (entity != null) {
                System.out.println("Response content length: " + entity.getContentLength());
            }
            JSONParser parser = new JSONParser();
            responseJsonObj = (JSONObject) parser.parse(EntityUtils.toString(entity));
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (org.json.simple.parser.ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            // When HttpClient instance is no longer needed,
            // shut down the connection manager to ensure
            // immediate deallocation of all system resources
            httpclient.getConnectionManager().shutdown();
        }
        return responseJsonObj;
    }

All you have to do is create a class with the variables ip, port , uname and password. IP is the IP of the system when Multichain daemon is running. Port is the RPC port assigned. uname is the user name which is by default multichain. password is the password for the RPC user. The user name and password are present in the multichain.conf file in the chain named directory. To check the RPC port check the value in params.dat.

By default Multichain only accepts request from local host, since you are using a different system you would need to add the IP of the system from the RPC calls are to be made by editing the multichain.conf file.

Append rpcallowip=192.168.1.0/24 at the end of the multichain.conf. The example will allow all IP's in the range of 192.168.1.*. However you can add multiple entries with associated IP's as well. For example if your system IP is 192.168.1.12, it could be done by adding   rpcallowip=192.168.1.12

The response from the method shall be a JSON object similar to the way we see the output using the Multichain-Cli . Use the params to pass extra info as required by a method.

After making any changes to the multichain.conf file, to make the change effective, a restart of the multichain daemon is required. 

answered Aug 2, 2016 by 7sigma
edited Aug 2, 2016 by 7sigma
Thank you very much for the example. I have been searching everywhere for this!

I do have just one minor confusion though... The method invokeRPC takes three parameters id, method and params. Method and params are clear to me but what does id represent?

For example if I wanted to invoke the liststreams method on my multichain server then how would I call invokeRPC

invokeRPC(?, "listsstreams", null);

I am not clear as to what to pass for the id parameter.

Thanks in advance for any help!
Not able to connect to MultiChain Node
...