Attempting to connect to Multichain using a Java Client - Need help to resolve error.

+2 votes

I am learning to use Multichain and tried sample code that I found to connect to Multichain blockchain. I am unable to connect and am receiving the error " The target server failed to respond ". The external IP address of server is and port is 8333. Used the login credentials found in multichain.conf. Thanks in advance.


The code is as follows:

<----- Code Starts here ---->

package com.srs.multichain.json_rpc_client;

import java.util.Arrays;
import java.util.List;
import java.util.UUID;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.ParseException;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;

public class RPCClient {

    private static final String COMMAND_GET_BALANCE = "getbalance";
    private static final String COMMAND_GET_INFO = "getinfo";
    private static final String COMMAND_GET_NEW_ADDRESS = "getnewaddress";

    private JSONObject invokeRPC(String id, String method, List<String> params) {
        DefaultHttpClient httpclient = new DefaultHttpClient();

        JSONObject json = new JSONObject();
        json.put("id", id);
        //json.put("chain_name", chainName);
        json.put("method", method);
        if (null != params) {
            JSONArray array = new JSONArray();
            json.put("params", params);
        JSONObject responseJsonObj = null;
        try {
            httpclient.getCredentialsProvider().setCredentials(new AuthScope("", 8333),
                    new UsernamePasswordCredentials("multichainrpc", "FMHYwtrpoKRmUNySdXKCi8QFVNpvvAtqDxAvhfWw9DmK"));
            StringEntity myEntity = new StringEntity(json.toJSONString());
            HttpPost httppost = new HttpPost("http://multichainrpc:FMHYwtrpoKRmUNySdXKCi8QFVNpvvAtqDxAvhfWw9DmK@");

            System.out.println("executing request" + httppost.getRequestLine());
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();

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

    public Double getBalance(String chainName, String account) {
        String[] params = { account };
        JSONObject json = invokeRPC(UUID.randomUUID().toString(), COMMAND_GET_BALANCE, Arrays.asList(params));
        return (Double)json.get("result");

    public String getNewAddress(String chainName, String account) {
        String[] params = { account };
        JSONObject json = invokeRPC(UUID.randomUUID().toString(), COMMAND_GET_NEW_ADDRESS, Arrays.asList(params));
        return (String)json.get("result");

    public JSONObject getInfo(String chainName) {
        JSONObject json = invokeRPC(UUID.randomUUID().toString(), COMMAND_GET_INFO, null);
        return (JSONObject)json.get("result");

<----- Code snippet ends here ----->

I am receiving the following error -

executing requestPOST http://multichainrpc:FMHYwtrpoKRmUNySdXKCi8QFVNpvvAtqDxAvhfWw9DmK@ HTTP/1.1
org.apache.http.NoHttpResponseException: The target server failed to respond
    at org.apache.http.impl.conn.DefaultResponseParser.parseHead(
    at org.apache.http.impl.AbstractHttpClientConnection.receiveResponseHeader(
    at org.apache.http.impl.conn.DefaultClientConnection.receiveResponseHeader(
    at org.apache.http.impl.conn.AbstractClientConnAdapter.receiveResponseHeader(
    at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(
    at org.apache.http.protocol.HttpRequestExecutor.execute(
    at org.apache.http.impl.client.DefaultRequestDirector.tryExecute(
    at org.apache.http.impl.client.DefaultRequestDirector.execute(
    at org.apache.http.impl.client.AbstractHttpClient.execute(
    at org.apache.http.impl.client.AbstractHttpClient.execute(
    at org.apache.http.impl.client.AbstractHttpClient.execute(
    at com.srs.multichain.json_rpc_client.RPCClient.invokeRPC(
    at com.srs.multichain.json_rpc_client.RPCClient.getInfo(
    at com.srs.multichain.json_rpc_client.App.main(
Exception in thread "main" java.lang.NullPointerException
    at com.srs.multichain.json_rpc_client.RPCClient.getInfo(
    at com.srs.multichain.json_rpc_client.App.main(

<-------------- End of stacktrace ---------------->


asked Jan 11, 2017 by ramassrs

2 Answers

+2 votes
Best answer
Hi All,

I found that I was using the wrong RPC port! Sorry for the inconvenience. The code I shared above does work. I had to use the "default-rpc-port" setting in the "params.dat" file. In my case it was set to 8332. I used 8333 instead. Port 8333 was the value for "default-network-port" instead.



answered Jan 11, 2017 by ramassrs
selected Jan 11, 2017 by ramassrs
Hi dear,
  I want to use your code to connect my multichain. But I don't understand what the FMHYwtrpoKRmUNySdXKCi8QFVNpvvAtqDxAvhfWw9DmK is?
 Thanks a lot
FMHYwtrpoKRmUNySdXKCi8QFVNpvvAtqDxAvhfWw9DmK is his "rpcpassword", you will find yours at multichain/chain/multichain.conf file.. All the best..
You are so kindful! I got it.
0 votes
I'm sorry,
I would like to try your code for my java app. But the java main method where did you put it?
answered Jan 30, 2019 by T.R