Issue while connecting with node using java client

+2 votes

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

http://bitcoin.stackexchange.com/questions/7529/how-to-communicate-between-java-and-bitcoind

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@192.168.136.128:6795

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();
    array.addAll(params);
    json.put("params", params);
}
JSONObject responseJsonObj = null;
try {
    httpclient.getCredentialsProvider().setCredentials(new AuthScope("192.168.136.128", 6795),
            new UsernamePasswordCredentials("", ""));
    StringEntity myEntity = new StringEntity(json.toJSONString());
    System.out.println(json.toString());
    HttpPost httppost = new HttpPost("http://192.168.136.128:6795");
    httppost.setEntity(myEntity);

    System.out.println("executing request" + httppost.getRequestLine());
    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());
        // 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
    e.printStackTrace();

 

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.
http://www.multichain.com/qa/714/trying-to-connect-via-java-client
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(
                Base64.encodeBase64(rpcURL.getUserInfo().getBytes(
                        Charset.forName("ISO8859-1"))));
            conn.addRequestProperty("Authorization", "Basic " + authStr);
answered Feb 23, 2016 by alex
...