I was going to post this to
https://bitcointalksearch.org/topic/sample-account-system-using-json-rpc-needed-417but I am a newbie and cannot.
Here it is:
// START OF FILE ----------------------------------------------------------------------------------------------
import java.io.*;
import java.net.*;
import java.nio.*;
import java.lang.management.*;
import java.util.*;
import org.json.simple.*;
import org.json.simple.parser.*;
public class
json_test {
public static void
main(String[] args){
JSONObject obj = new JSONObject();
obj.put("method", "getinfo");
obj.put("id", new Integer(11));
String btc_comm = obj.toString();
try{
Authenticator.setDefault(new BitCoinAuthenticator());
URL url_bcd = new URL("
http://localhost:8332/");
URLConnection yc = url_bcd.openConnection();
yc.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(yc.getOutputStream());
wr.write(btc_comm);
wr.flush();
BufferedReader in = new BufferedReader(
new InputStreamReader(
yc.getInputStream()));
String inputLine;
while((inputLine = in.readLine()) != null){
System.out.println(inputLine);
}
in.close();
} catch (MalformedURLException ex) {
System.out.println(ex);
} catch (IOException ex) {
System.out.println(ex);
}
}
}
class BitCoinAuthenticator extends Authenticator {
// This method is called when a password-protected URL is accessed
protected PasswordAuthentication getPasswordAuthentication() {
String promptString = getRequestingPrompt();
String hostname = getRequestingHost();
InetAddress ipaddr = getRequestingSite();
int port = getRequestingPort();
String username = ""; // i have no user name in /home/your_user/.bitcoin/bitcoin.conf
String password = "none"; // rpcpassword=none in /home/your_user/.bitcoin/bitcoin.conf
return new PasswordAuthentication(username, password.toCharArray());
}
}
// END OF FILE ----------------------------------------------------------------------------------------------
I compile with this script:
# START OF FILE
rm ./json_test.class
PS_CLASS_PATH=./lib/JSON/*
javac -classpath $PS_CLASS_PATH json_test.java
echo "Type enter \n Thanks." | more -1
# END OF FILE
And run it with this script:
# START OF FILE
PS_CLASS_PATH=./lib/JSON/*
java -cp $PS_CLASS_PATH json_test
echo "Type enter \n Thanks." | more -1
# END OF FILE
Preconditions:
1)
json-simple-1.1.1.jar
file must be in ./lib/JSON
(you can google it).
2)
bitcoind must be running and you must be able to type the command
bitcoind getinfo
and get something like:
{
"version" : 60300,
"protocolversion" : 60001,
"walletversion" : 60000,
"balance" : 0.00000000,
"blocks" : 198343,
"connections" : 8,
"proxy" : "",
"difficulty" : 2694047.95295501,
"testnet" : false,
"keypoololdest" : 1346473056,
"keypoolsize" : 101,
"paytxfee" : 0.00000000,
"errors" : ""
}
Hope it helps.
JLQ.