2

您好,我正在尝试使用低级库编年史线发送 http 请求。我能够通过使用 java 套接字来完成这项工作,但我对使用这个库的差异感到好奇。下面是调用代码:

private static void call() throws IOException, TimeoutException {
        final String desc = "google.com";
        TCPRegistry.createServerSocketChannelFor(desc);

        EventLoop eg = new EventGroup(true);
        eg.start();
        TcpChannelHub tcpChannelHub = new TcpChannelHub(null, eg, WireType.TEXT, "",
                SocketAddressSupplier.uri(desc), false, null, HandlerPriority.HIGH, new FatalFailureConnectionStrategy(0, false));
        final long tid = tcpChannelHub.nextUniqueTransaction(System.currentTimeMillis());

        final Wire wire = new TextWire(Bytes.elasticByteBuffer());

        wire.writeDocument(true, w -> w.write("tid").int64(tid));
        wire.write("GET / HTTP/1.1\\r\\n");
        wire.write("Host google.com\\r\\n");
        tcpChannelHub.writeSocket(wire, false, false);
        tcpChannelHub.proxyReply(TimeUnit.SECONDS.toMillis(1), tid);
    }

我收到以下错误:

20:26:43.275 [main/TcpChannelHub-Reads--(none)] DEBUG net.openhft.chronicle.network.connection.TcpChannelHub - connected to remoteAddress=(none)
20:26:43.279 [main] DEBUG net.openhft.chronicle.network.connection.TcpChannelHub - tid=1606937203253 of client request
20:26:44.279 [main] WARN net.openhft.chronicle.network.connection.TcpChannelHub - 
java.util.concurrent.TimeoutException: timeoutTimeMs=1000
    at net.openhft.chronicle.network.connection.TcpChannelHub$TcpSocketConsumer.syncBlockingReadSocket(TcpChannelHub.java:1210)
    at net.openhft.chronicle.network.connection.TcpChannelHub.proxyReply(TcpChannelHub.java:685)
    at com.example.demo.DemoApplication.call(DemoApplication.java:115)
    at com.example.demo.DemoApplication.main(DemoApplication.java:31)
20:26:44.280 [main] DEBUG net.openhft.chronicle.network.connection.TcpChannelHub - closing
net.openhft.chronicle.core.StackTrace: only added for logging - please ignore ! on main
    at net.openhft.chronicle.network.connection.TcpChannelHub.closeSocket(TcpChannelHub.java:502)
    at net.openhft.chronicle.network.connection.TcpChannelHub.proxyReply(TcpChannelHub.java:693)
    at com.example.demo.DemoApplication.call(DemoApplication.java:115)
    at com.example.demo.DemoApplication.main(DemoApplication.java:31)
20:26:44.281 [main] DEBUG net.openhft.chronicle.network.connection.TcpChannelHub - disconnected to remoteAddress=(none)

Exception in thread "main" java.util.concurrent.TimeoutException: timeoutTimeMs=1000

1000ms 应该足够了,所以问题出在配置的某个地方。

4

1 回答 1

0

虽然我确信你可以让它工作,但它不是与 HTTP 一起使用的自然选择。

但是,如果这只是一个练习,您应该避免writingDocument ,因为这会在 HTTP 不期望的内容之前添加一个 32 位长度。

createServerSocketChannelFor方法用于生成一个虚拟服务器以进行测试,因此它会阻止您联系您想到的服务器。

我建议您使用为 HTTP 设计的库(例如 Java 内置的库)来实现这一点。

于 2020-12-03T15:26:55.813 回答