Issue getting JSON-RPC response using Java client

+1 vote

So I am trying to write a Java client that can communicate to the multichain server. The server is running on an Ubuntu VM (VM1) and I am writing my code on another Ubuntu VM (VM2).

Both VMs are on the same network and can communicate with one another. I can run multichain-cli commands from VM2 and get a successful response.

i was following the example from and I have the following code;


public JSONObject invokeRPC(String id, String method, List<Object> params, String chainName){
    HttpClient httpClient = HttpClientBuilder.create().build();
    JSONObject jsonObject = new JSONObject();
    jsonObject.put("id", id);
    jsonObject.put("method", method);
    if(params != null && params.size() != 0){
        jsonObject.put("params", params);
    jsonObject.put("chain_name", chainName);
    JSONObject responseJSONObject = new JSONObject();
        CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
                new AuthScope("", 9732),
                new UsernamePasswordCredentials("multichainrpc", "EZikv3MtoKA2yjrG6T7eTkPZMXntwr9k1ft7ja3jLLaA")
        StringEntity myEntity = new StringEntity(jsonObject.toString());

        HttpPost httpPost = new 
HttpPost("");       //HttpPost("http://multichainrpc:EZikv3MtoKA2yjrG6T7eTkPZMXntwr9k1ft7ja3jLLaA@");

        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();


        if(httpEntity != null){
            System.out.println("Response content length: " + httpEntity.getContentLength());

        responseJSONObject = new JSONObject().getJSONObject(EntityUtils.toString(httpEntity));

    } catch (UnsupportedEncodingException e) {
    } catch (ClientProtocolException e) {
    } catch (IOException e) {
    } finally {

    return responseJSONObject;

The code fails at 'HttpResponse httpResponse = httpClient.execute(httpPost);' with the following Exception;

org.apache.http.conn.HttpHostConnectException: Connect to [/] failed: Connection refused (Connection refused)

at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(

at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(

at org.apache.http.impl.execchain.MainClientExec.establishRoute(

at org.apache.http.impl.execchain.MainClientExec.execute(

at org.apache.http.impl.execchain.ProtocolExec.execute(

at org.apache.http.impl.execchain.RetryExec.execute(

at org.apache.http.impl.execchain.RedirectExec.execute(

at org.apache.http.impl.client.InternalHttpClient.doExecute(

at org.apache.http.impl.client.CloseableHttpClient.execute(

at org.apache.http.impl.client.CloseableHttpClient.execute(

at org.apache.http.impl.client.CloseableHttpClient.execute(

at com.recordz.general.connect.Connect.invokeRPC(

at com.recordz.general.connect.Connect.main(

Caused by: Connection refused (Connection refused)

at Method)






at org.apache.http.conn.socket.PlainConnectionSocketFactory.connectSocket(

at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(

... 12 more


I am not sure why this is happening as both VMs are on the same LAN and can communicate with one another.

Any advice would be much appreciated

asked Jul 8, 2017 by doldy101

1 Answer

+1 vote
So I found the solution.

On the client vm I had to edit the multichain.conf file in the blockchain directory with the rpcallowip entry.

I had previously done this, but on the blockchain vm VM1.
answered Jul 9, 2017 by doldy101