我在比特币stackexchange中问了同样的问题。在这里,我重新发布它以吸引更多的观众。
我在堆栈交换How can I code a Bitcoin JSON-RPC “getwork” request in Java?
中经历了这篇文章我试着为getwork json rpc写一个简单的片段。
public static void main(String[] args) throws Exception {
String request = "{\"method\": \"getwork\", \"params\": [], \"id\":0}";
URL url = new URL("http://de01.supportxmr.com:7777");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
if (conn .getConnectTimeout() == 0)
conn.setConnectTimeout(1000);
if (conn.getReadTimeout() == 0)
conn.setReadTimeout(1000);
conn.setRequestMethod("POST");
String encoded = Base64.getEncoder().encodeToString(("<my_wallet_addr>:x").getBytes(StandardCharsets.UTF_8)); //Java 8
conn.setRequestProperty("Authorization", "Basic "+encoded);
conn.setRequestProperty("Accept", "application/json");
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("Content-Length", Integer.toString(request.getBytes().length));
conn.setRequestProperty("X-Mining-Extensions", "midstate");
conn.setAllowUserInteraction(false);
conn.setUseCaches(false);
conn.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
wr.writeBytes(request);
wr.close();
InputStream is = conn.getInputStream();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
int len;
byte[] buffer = new byte[4096];
while ((len = is.read(buffer)) != -1) {
bos.write(buffer, 0, len);
}
String content = bos.toString();
is.close();
System.out.println(content);
}
当我运行此代码时,出现错误
Exception in thread "main" java.net.SocketException: Unexpected end of file from server
at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:792)
at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:647)
at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:789)
at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:647)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1536)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1441)
at org.litecoinpool.miner.Test.main(Test.java:42)
我在这里缺少什么?是否需要在机器上运行stratum proxy?如果是这样,我如何指定要在java代码中运行的参数?
按照建议将getwork更改为getblocktemplate / stratum后,我尝试了直接TCP连接到服务器。
public static void main(String[] args) throws Exception {
String message1 = "{\"id\":1,\"method\":\"mining.subscribe\",\"params\":[]}";
String authorizemessage = "{\"params\": [\"<wallet_address>\", \"x\"], \"id\": 2, \"method\": \"mining.authorize\"}";
Socket soc = new Socket("de01.supportxmr.com", 7777);
System.out.println("connected");
OutputStream outputStream = soc.getOutputStream();
outputStream.write(authorizemessage.getBytes());
outputStream.flush();
BufferedReader in = new BufferedReader(new InputStreamReader(soc.getInputStream()));
JSONObject json = new JSONObject(in.readLine());
System.out.println("json response: " + json.toString());
outputStream.write(message1.getBytes());
outputStream.flush();
in = new BufferedReader(new InputStreamReader(soc.getInputStream()));
json = new JSONObject(in.readLine());
System.out.println("json response: " + json.toString());
}
但又没有运气:(