Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import org.apache.http.HttpEntity;
- import org.apache.http.auth.AuthScope;
- import org.apache.http.auth.UsernamePasswordCredentials;
- import org.apache.http.client.methods.CloseableHttpResponse;
- import org.apache.http.client.methods.HttpPost;
- import org.apache.http.entity.StringEntity;
- import org.apache.http.impl.client.BasicCredentialsProvider;
- import org.apache.http.impl.client.CloseableHttpClient;
- import org.apache.http.impl.client.HttpClientBuilder;
- import org.apache.http.util.EntityUtils;
- import org.json.simple.JSONArray;
- import org.json.simple.JSONObject;
- import org.json.simple.parser.JSONParser;
- import java.io.IOException;
- import java.util.UUID;
- /**
- Maven:
- <dependencies>
- <dependency>
- <groupId>org.apache.httpcomponents</groupId>
- <artifactId>httpclient</artifactId>
- <version>4.3.4</version>
- </dependency>
- <dependency>
- <groupId>com.googlecode.json-simple</groupId>
- <artifactId>json-simple</artifactId>
- <version>1.1.1</version>
- </dependency>
- </dependencies>
- conf:
- rpcuser=user1
- rpcpassword=pB341z
- rpcallowip=192.168.0.*
- rpcport=5557
- listen=1
- daemon=1
- server=1
- */
- public class CoinSender {
- public static void main(String[] args) throws Exception {
- String ip = "192.168.0.41";
- int port = 5557;
- String targetAddress = "SDid48TEECEu7kaGFtH1i3jHEoJkiGXqPk";
- String user = "user1";
- String pass = "pB341z";
- RPCClient rpcClient = new RPCClient(ip, port, user, pass);
- int total = rpcClient.getBalance().intValue();
- int amount = 5;
- for(; total > 0; total -= amount) {
- JSONObject jsonObject = rpcClient.sendCoins(targetAddress, amount);
- System.out.println(total + ": " + jsonObject);
- Thread.sleep(60000);
- }
- rpcClient.close();
- }
- public static class RPCClient {
- private final CloseableHttpClient httpclient;
- private String ip;
- private int port;
- public RPCClient(String ip, int port, String user, String pass) {
- this.ip = ip;
- this.port = port;
- BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
- credentialsProvider.setCredentials(new AuthScope(ip, port), //12789
- new UsernamePasswordCredentials(user, pass));
- httpclient = HttpClientBuilder.create()
- .setDefaultCredentialsProvider(credentialsProvider)
- .build();
- }
- public JSONObject invokeRPC(String id, String method, Object ...params) {
- JSONObject requestJSON = new JSONObject();
- requestJSON.put("id", id);
- requestJSON.put("method", method);
- if (null != params) {
- JSONArray array = new JSONArray();
- for (Object param : params) {
- array.add(param);
- }
- requestJSON.put("params", array);
- }
- System.out.println(requestJSON);
- JSONObject responseJSON = null;
- try {
- StringEntity myEntity = new StringEntity(requestJSON.toJSONString());
- HttpPost httppost = new HttpPost("http://" + ip + ":" + port);
- httppost.setEntity(myEntity);
- CloseableHttpResponse response = httpclient.execute(httppost);
- HttpEntity entity = response.getEntity();
- JSONParser parser = new JSONParser();
- responseJSON = (JSONObject) parser.parse(EntityUtils.toString(entity));
- response.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- return responseJSON;
- }
- public void close() throws IOException {
- httpclient.close();
- }
- public Double getBalance() {
- JSONObject json = invokeRPC(UUID.randomUUID().toString(), "getbalance");
- System.out.println("DEBUG, getBalance: " + json);
- return (Double)json.get("result");
- }
- public JSONObject sendCoins(String to, float amount) {
- return invokeRPC(UUID.randomUUID().toString(), "sendtoaddress", to, amount);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement