![](/img/trans.png)
[英]Simple JSON-RPC call to Bitcoin wallet with Apache Http Client 4 gives “failed to respond”
[英]Can't connect to Bitcoin wallet with JSON-RPC
我正在尝试从Java连接到比特币钱包。 但是我得到网络异常:服务器重定向太多次。 如果有人帮助我理解我将非常高兴。 这是我的代码:
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());
}
和.conf文件:
rpcuser="admin"
rpcpassword="admin"
rpcallowip=*
rpcport=44843
server=1
daemon=1
listen=1
rpcconnect=127.0.0.1
如果这些是您的代码和.conf,请在您的conf文件中删除"
。或在字符串中添加\\"
。 所以这
return new PasswordAuthentication ("\"admin\"", "\"admin\"".toCharArray());
或这个
rpcuser="admin"
rpcpassword="admin"
另外,您的密码或用户名中不能包含#(这是我的情况)。
找到这个之后,我得到了一个Invalid JSON-RPC 2.0 response
。
我的解决方案是将函数public JSONRPC2Response parseJSONRPC2Response(final String jsonString)
!jsonObject.containsKey("error")
更改为(!jsonObject.containsKey("error") || jsonObject.get("error") == null)
。它位于495行附近的JSONRPC2Parser.java中的JSONRPC 2.0的基础中。
编辑:这是在DogeCoin-QT上的,所以也许Bitcoin-QT没有这个问题。
好的,我解决了这个问题:
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();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.