<p>In this line:</p>

<pre><code>        HttpPost httppost = new HttpPost("http://198.154.*.*:34907");
</code></pre>

<p>the address <code>198.154.*.*</code> is not valid IP address syntax. If that is really how your source code looks, then you need to use a full, valid IP address which is all numbers (for example, <code>198.154.1.1</code> or whatever is appropriate for your network).</p>

<p>Even if you're deliberately obscuring your IP address for us, you still have a problem because you have set <code>rpcallowip=127.0.0.1</code> which <em>only</em> allows that address to talk to the RPC server. Add another <code>rpcallowip</code> line that contains the address of the machine where you are running this Java code.</p>

CC BY-SA 3.0
2014-05-01T11:22:31.597Z
HttpPost httppost = new HttpPost("http://198.154.*.*:34907"); - I have hidden full ip address. There are numbers instead "*".
Vitali Grabovski
CC BY-SA 3.0
2014-04-30T20:48:28.710Z
2014-04-30T20:48:28.710Z
Greg Hewgill
<p>I am using Ubuntu Server 12.04. My *.conf file:</p>

<pre><code>...
# Enable RPC
server=1

# Uncomment to allow localhost to use RPC
rpcallowip=127.0.0.1

# RPC information
# THIS MUST BE CHANGED FOR YOUR SECURITY
rpcuser=username
rpcpassword=passssssssss_new

# Mining is initially disabled
# gen=0
...
</code></pre>

<p>Java RPCClient that I use:</p>

<pre><code>import java.io.IOException;
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&lt;String&gt; params) {

//      CloseableHttpClient httpclient = HttpClientBuilder.create().build();
//      
//      httpclient.getCredentialsProvider();
        DefaultHttpClient httpclient = new DefaultHttpClient();

        JSONObject json = new JSONObject();
        json.put("id", id);
        json.put("method", method);
        if (null != params) {
            JSONArray array = new JSONArray();
            array.addAll(params);
            json.put("params", params);
        }
        JSONObject responseJsonObj = null;
        try {
            httpclient.getCredentialsProvider().setCredentials(new AuthScope("198.154.*.*", 34907),
                    new UsernamePasswordCredentials("username", "passssssssss_new"));
            StringEntity myEntity = new StringEntity(json.toJSONString());
            System.out.println(json.toString());
            HttpPost httppost = new HttpPost("http://198.154.*.*:34907");
            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();
        } 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;
    }

    public Double getBalance(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 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() {
        JSONObject json = invokeRPC(UUID.randomUUID().toString(), COMMAND_GET_INFO, null);
        return (JSONObject)json.get("result");
    }

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

    /*public static void main(String[] args) {
        System.out.println(new RPCClient().getInfo());      
    }*/
}
</code></pre>

<p>Current result: </p>

<pre><code>{"id":"60f910c6-a893-4753-a9d3-cbe6973ccb14","method":"getinfo"}
executing requestPOST http://198.154.*.*:34907 HTTP/1.1
----------------------------------------
HTTP/1.1 403 Forbidden
Response content length: 0
Unexpected token END OF FILE at position 0.
    at org.json.simple.parser.JSONParser.parse(JSONParser.java:257)
    at org.json.simple.parser.JSONParser.parse(JSONParser.java:81)
    at org.json.simple.parser.JSONParser.parse(JSONParser.java:75)
    at RPCClient.invokeRPC(RPCClient.java:61)
    at RPCClient.getInfo(RPCClient.java:96)
    at KeccakTest.main(KeccakTest.java:125)
Exception in thread "main" java.lang.RuntimeException: java.lang.NullPointerException
    at KeccakTest.main(KeccakTest.java:129)
Caused by: java.lang.NullPointerException
    at RPCClient.getInfo(RPCClient.java:97)
    at KeccakTest.main(KeccakTest.java:125)
</code></pre>

CC BY-SA 3.0
2014-04-30T08:45:27.597Z
@George Kimionis, "198.154.xxx.xxx" is ip of the server, 34907 - port for JSON-RPC connections.
Vitali Grabovski
CC BY-SA 3.0
2014-05-01T11:24:58.770Z
@George Kimionis, thanks! "rpcallowip=*" was helpful.
Vitali Grabovski
<bitcoind><json-rpc><java>
http post via java to bitcoind does not work
Vitali Grabovski
# http post via java to bitcoind does not work

(Asked by: username_0 on 2014-04-30)

I am using Ubuntu Server 12.04. My \*.conf file:

```
...
# Enable RPC
server=1

# Uncomment to allow localhost to use RPC
rpcallowip=127.0.0.1

# RPC information
# THIS MUST BE CHANGED FOR YOUR SECURITY
rpcuser=username
rpcpassword=passssssssss_new

# Mining is initially disabled
# gen=0
...

```

Java RPCClient that I use:

```
import java.io.IOException;
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) {

//      CloseableHttpClient httpclient = HttpClientBuilder.create().build();
//      
//      httpclient.getCredentialsProvider();
        DefaultHttpClient httpclient = new DefaultHttpClient();

        JSONObject json = new JSONObject();
        json.put("id", id);
        json.put("method", method);
        if (null != params) {
            JSONArray array = new JSONArray();
            array.addAll(params);
            json.put("params", params);
        }
        JSONObject responseJsonObj = null;
        try {
            httpclient.getCredentialsProvider().setCredentials(new AuthScope("198.154.*.*", 34907),
                    new UsernamePasswordCredentials("username", "passssssssss_new"));
            StringEntity myEntity = new StringEntity(json.toJSONString());
            System.out.println(json.toString());
            HttpPost httppost = new HttpPost("http://198.154.*.*:34907");
            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();
        } 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;
    }

    public Double getBalance(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 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() {
        JSONObject json = invokeRPC(UUID.randomUUID().toString(), COMMAND_GET_INFO, null);
        return (JSONObject)json.get("result");
    }

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

    /*public static void main(String[] args) {
        System.out.println(new RPCClient().getInfo());      
    }*/
}

```

Current result:

```
{"id":"60f910c6-a893-4753-a9d3-cbe6973ccb14","method":"getinfo"}
executing requestPOST http://198.154.*.*:34907 HTTP/1.1
----------------------------------------
HTTP/1.1 403 Forbidden
Response content length: 0
Unexpected token END OF FILE at position 0.
    at org.json.simple.parser.JSONParser.parse(JSONParser.java:257)
    at org.json.simple.parser.JSONParser.parse(JSONParser.java:81)
    at org.json.simple.parser.JSONParser.parse(JSONParser.java:75)
    at RPCClient.invokeRPC(RPCClient.java:61)
    at RPCClient.getInfo(RPCClient.java:96)
    at KeccakTest.main(KeccakTest.java:125)
Exception in thread "main" java.lang.RuntimeException: java.lang.NullPointerException
    at KeccakTest.main(KeccakTest.java:129)
Caused by: java.lang.NullPointerException
    at RPCClient.getInfo(RPCClient.java:97)
    at KeccakTest.main(KeccakTest.java:125)

```

**Tags:** bitcoind, json-rpc, java

> **Comments:**
> **username_0**: @George Kimionis, "198.154.xxx.xxx" is ip of the server, 34907 - port for JSON-RPC connections.  
> **username_0**: @George Kimionis, thanks! "rpcallowip=*" was helpful.  

---

## 1 answers

### Answer by username_1 on 2014-04-30. Score: 1

In this line:

```
        HttpPost httppost = new HttpPost("http://198.154.*.*:34907");

```

the address `198.154.*.*` is not valid IP address syntax. If that is really how your source code looks, then you need to use a full, valid IP address which is all numbers (for example, `198.154.1.1` or whatever is appropriate for your network).

Even if you're deliberately obscuring your IP address for us, you still have a problem because you have set `rpcallowip=127.0.0.1` which *only* allows that address to talk to the RPC server. Add another `rpcallowip` line that contains the address of the machine where you are running this Java code.

> **Comments:**
> **username_0**: HttpPost httppost = new HttpPost("http://198.154.*.*:34907"); - I have hidden full ip address. There are numbers instead "*".  

---

