Issue while connecting with node using java client

+2 votes

I followed the instructions given in below page but still getting the error.

When I started a node on one machine it gets successfully started and give the message as 

New users can connect to this blockchain using
multichaind chain2@

Now from another machine using java code, i tried to connect with using below sample code but it gives connection timeout message. Also in below code, I don't what credential i have to provide while initializing UsernamePasswordCredentials(). 

DefaultHttpClient httpclient = new DefaultHttpClient();
JSONObject json = new JSONObject();
json.put("id", "chain2");
json.put("method", "getinfo");
List<String> params = new ArrayList<String>()
if (null != params) {
    JSONArray array = new JSONArray();
    json.put("params", params);
JSONObject responseJsonObj = null;
try {
    httpclient.getCredentialsProvider().setCredentials(new AuthScope("", 6795),
            new UsernamePasswordCredentials("", ""));
    StringEntity myEntity = new StringEntity(json.toJSONString());
    HttpPost httppost = new HttpPost("");

    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


asked Oct 25, 2015 by Abhi
I also have the same problem, I can not connect using java api by another machine. Let me know if you can solve please. Thanks.
You want to use jsonrpc4j.

1 Answer

+1 vote
HttpURLConnection conn = (HttpURLConnection) yourURL.openConnection();          
            conn.addRequestProperty("Content-Type", "application/json-rpc");

URL rpcURL = new URL("http://" + user + ':' + password + "@"
                            + host + ":" + port) + "/");

String authStr = new String(
            conn.addRequestProperty("Authorization", "Basic " + authStr);
answered Feb 23, 2016 by alex