I want to connect to bitcoind using java. My plan is to use htmlunit and gson. Right now I can complete a single request successfully. However, I can not do more than exactly four subsequent requests due to a SocketTimeoutException being thrown at the fifth request.
I tried:
- Waiting between requests. (No effect visible)
- Forcing a failing http status code e.g. by requesting
getinfoo
instead ofgetinfo
. (I do not get the timeout after four failing requests)
Any help or comment is appreciated!
package test;
import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.HttpMethod;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.WebRequest;
import com.google.gson.Gson;
import java.io.IOException;
import java.net.SocketTimeoutException;
import java.net.URL;
public class Test {
public static void main(String[] args) {
WebClient client = new WebClient(BrowserVersion.FIREFOX_24);
// http://[bitcoind-user]:[password]@localhost:[bitcoind-port]
String baseUrl = "http://admin:admin@localhost:8332/";
client.getOptions().setTimeout(2000);
while (true) {
try {
WebRequest req = new WebRequest(new URL(baseUrl));
req.setAdditionalHeader("Content-type", "application/json");
req.setHttpMethod(HttpMethod.POST);
JSONRequestBody body = new JSONRequestBody();
body.setMethod("getinfo");
req.setRequestBody(new Gson().toJson(body, JSONRequestBody.class));
client.getPage(req);
client.closeAllWindows();
System.out.println("ok. (No Exception)");
} catch (SocketTimeoutException tex) {
System.out.println("not ok: SocketTimeoutException");
} catch (IOException ex) {
System.out.println("not ok: IOException");
} catch (FailingHttpStatusCodeException hex) {
System.out.println("not ok: FailingHttpStatusCodeException");
}
}
}
}
(EDIT:)
Removing the .setTimeout(2000)
did not help as well. Measuring the time for each request shows that they are quite quick:
ok. (No Exception) Timer: 1.161 seconds
ok. (No Exception) Timer: 0.112 seconds
ok. (No Exception) Timer: 0.115 seconds
ok. (No Exception) Timer: 0.075 seconds
not ok: SocketTimeoutException Timer: 90.119 seconds
not ok: SocketTimeoutException Timer: 90.145 seconds
not ok: SocketTimeoutException Timer: 90.134 seconds
Repeating same request multiple times in the terminal using curl
took always less than one second so bitcoind
should not be causing the problems.