Question

I'm trying to connect to Bitcoin wallet from Java. But i get network exception: Server redirected too many times. I would be very glad if someone helps me understand. Here's my code:

public static void SetupRPC() {
    CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL));
    Authenticator.setDefault(new Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication ("admin", "admin".toCharArray());
        }
    });

    URL serverURL = null;
    try {
        serverURL = new URL("http://127.0.0.1:44843");
    } catch (MalformedURLException e) {
        System.err.println(e.getMessage());
    }

    JSONRPC2Session mySession = new JSONRPC2Session(serverURL);
    String method = "getinfo";
    int requestID = 0;
    JSONRPC2Request request = new JSONRPC2Request(method, requestID);

    // Send request
    JSONRPC2Response response = null;

    try {
        response = mySession.send(request);
    } catch (JSONRPC2SessionException e) {
        System.err.println(e.getMessage());
    }

    if (response.indicatesSuccess())
        System.out.println(response.getResult());
    else
        System.out.println(response.getError().getMessage());
}

And .conf file:

rpcuser="admin"
rpcpassword="admin"
rpcallowip=*
rpcport=44843
server=1
daemon=1
listen=1
rpcconnect=127.0.0.1
Was it helpful?

Solution 2

Ok, i solve this problem:

Authenticator.setDefault(new Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication("user", "pass".toCharArray());
        }
    });
    String uri = "http://127.0.0.1:8332";
    String requestBody = "{\"jsonrpc\":\"2.0\",\"id\":\"1\",\"method\":\"getbalance\"}";
    String contentType = "application/json";
    HttpURLConnection connection = null;
    try {
        URL url = new URL(uri);
        connection = (HttpURLConnection) url.openConnection(Proxy.NO_PROXY);
        connection.setDoOutput(true);
        connection.setRequestMethod("POST");
        connection.setRequestProperty("Content-Type", contentType);
        connection.setRequestProperty("Accept", "application/json");
        connection.setRequestProperty("Content-Length", Integer.toString(requestBody.getBytes().length));
        connection.setUseCaches(true);
        connection.setDoInput(true);
        OutputStream out = connection.getOutputStream();
        out.write(requestBody.getBytes());
        out.flush();
        out.close();
    } catch (IOException ioE) {
        connection.disconnect();
        ioE.printStackTrace();
    }

    try {
        if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
            InputStream is = connection.getInputStream();
            BufferedReader rd = new BufferedReader(new InputStreamReader(is));
            String line;
            StringBuffer response = new StringBuffer();
            while ((line = rd.readLine()) != null) {
                response.append(line);
                response.append('\r');
            }
            rd.close();
            System.out.println(response);
        } else {
            connection.disconnect();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

OTHER TIPS

If these are your code and .conf, then remove the " in your conf file. Or add \" to your string. So this

return new PasswordAuthentication ("\"admin\"", "\"admin\"".toCharArray());

or this

rpcuser="admin"
rpcpassword="admin"

Also you cannot have # in your password or username(which was my case).

After I found this I got an Invalid JSON-RPC 2.0 response.

My solution was to change !jsonObject.containsKey("error") to (!jsonObject.containsKey("error") || jsonObject.get("error") == null) in the function public JSONRPC2Response parseJSONRPC2Response(final String jsonString) which is located in the base of JSONRPC 2.0 in JSONRPC2Parser.java somewhere around line 495.

EDIT: This was on the DogeCoin-QT, so maybe the Bitcoin-QT does not have this problem.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top