我想使用 java 连接到 bitcoind。我的计划是使用 htmlunit 和 gson。现在我可以成功完成一个请求。但是,由于在第五个请求中抛出了 SocketTimeoutException,我只能执行四个后续请求。
我试过了:
- 在请求之间等待。(看不到效果)
- 强制失败的 http 状态代码,例如通过请求
getinfoo
而不是getinfo
. (四个失败的请求后我没有得到超时)
任何帮助或评论表示赞赏!
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");
}
}
}
}
(编辑:)
删除.setTimeout(2000)
也没有帮助。测量每个请求的时间表明它们非常快:
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
在终端使用多次重复相同的请求curl
总是不到一秒,所以bitcoind
不应该导致问题。